我正在設置一個小部件,設置重複警報刷新。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。