2011-03-31 45 views
0

我正在設置一個小部件,設置重複警報刷新。android:當onDeleted()爲AppWidgetProvider時刪除所有警報

要設置這些重複的警報,在配置活動,在OnClickListener(),我做的:

 Intent widgetUpdate = new Intent(); 
     widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
     widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId); 
     widgetUpdate.setData(Uri.withAppendedPath(Uri.parse("wd_widget://widget/id/"), String.valueOf(mAppWidgetId))); 

     PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT); 
     AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     int mins = 60; 
     alarms.cancel(newPending); 
     alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), mins*1000*60,newPending); 

而且我的AppWidgetProvider收到此意圖在的onReceive(),並刷新視圖:

if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(intent.getAction())) { 
      Bundle extras = intent.getExtras(); 
      if(extras!=null) { 
       int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); 
       updateAppWidget(context, AppWidgetManager.getInstance(context), widgetId); 
      } 

我的問題: 當我殺了Widget,報警保持活躍althought我的AppWidgetProvider的onDeleted()做的事:

Intent widgetUpdate = new Intent(); 
    widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE); 
    PendingIntent newPending = PendingIntent.getBroadcast(context, 0, widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
    for (int appWidgetId : appWidgetIds) {  
     Log.i("DEBUG","Removing Alarms"); 
     alarms.cancel(newPending); 
    } 

那麼如何在離開widget的時候殺掉所有的鬧鐘? Best, Christophe。

回答

1

我認爲你需要發送同樣的意圖來取消警報,即在onDeleted,你沒有發送appWidgetId。

1

創建PendingIntent時,請爲「請求代碼」使用非零值。文檔說它沒有使用,但在我的應用程序中,我必須設置這個值。取消警報時,我使用相同的值清除警報。