2012-04-15 3 views
0

내가 개발하고 안드로이드 위젯입니다 확인하고 나는 위젯은 사용자가 자신의 홈 화면에 내 위젯을 추가 한 경우 그는 자신의 화면으로 다시 추가 할 수 없어야 즉, 단수 수 있음을 원하는단수 안드로이드 위젯

+1

왜 당신이로, 그 방법에서 사용자를 제한 할 것 좋은 경험 법칙, 사용자가 원하는 것을 수행하지 못하게하거나, 사용자를 돕고, 도와주고, 쉽게 만들 수있는 다른 앱을 찾지 마십시오. 사용자 기반을 유지하고 성장시키는 방법입니다. 와이 우리의 응용 프로그램 :) – erbsman

+0

내 응용 프로그램에서 위젯 이상을 사용하면 응용 프로그램에 심각한 손상을 입힐 수 있습니다! – enGMzizo

답변

1

) 위젯의 아이디를 추적 할 수의 AppWidgetProvider에

1)가 처음

2를 null로 설정, 공공 정적 정수 변수를 만들기위한 구성 작업을 만들기 : 당신이 이것을 달성하기 위해 시도 할 수있는 해결 방법은 위젯 (여기에 대한 지침은 https://developer.android.com/guide/topics/appwidgets/index.html#Configuring)

3) 구성 활동에서 공급자의 변수 상태를 확인하십시오. 위젯이 이미 생성 된 경우 (id가 null이 아님) AlertDialog를 사용하여 사용자에게 메시지를 표시 한 다음 결과를 취소로 설정할 수 있습니다.

코드는 다음과 같습니다

을 공급자에서 :

public class MyAppWidgetProvider extends AppWidgetProvider { 
    public static Integer widgetId; 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
     //If the current widget has been deleted, set widget id to null again 
     for(int i = 0; i < appWidgetIds.length; i++){ 
      if(widgetId == appWidgetIds[i]){ 
       widgetId = null; 
      } 
     } 
     super.onDeleted(context, appWidgetIds); 
    } 
} 

를 구성 활동에

public class ConfigurationActivity extends Activity { 
    public int appWidgetId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Intent intent = getIntent(); 
     Bundle extras = intent.getExtras(); 
     if (extras != null) { 
      appWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
     } 

     if(MyAppWidgetProvider.widgetId != null){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setMessage("Widget already created"); 
      builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        Intent resultValue = new Intent(); 
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
        setResult(RESULT_CANCELED, resultValue); 
        finish(); 
       } 
      }); 
      AlertDialog dialog = builder.create(); 
      dialog.show(); 
     } else { 
      MyAppWidgetProvider.widgetId = appWidgetId; 
      Intent resultValue = new Intent(); 
      resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); 
      setResult(RESULT_OK, resultValue);  
      finish(); 
     } 
    } 
}