2016-06-12 4 views
0

私はAndroidリマインダーを作ろうとしましたが、何かが間違っていました。私のアプリで繰り返し通知を設定すると、通知が消えなくなるまでアプリがアンインストールされます(繰り返し通知を削除してもその通知は消えません)。これを考慮して、私のリマインダーはデータベースを削除するとデータベースから削除されます。Androidの通知を繰り返しても消えた後でも消えます

この問題は、繰り返しアラームが発生した場合でも発生します。

どうすればよいですか?

//データベースクラスでの削除クエリ

public boolean deleteReminder(long rowId) { 

    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
} 

//私は私のメインの活動にリマインダーを削除する方法

@Override 
public boolean onContextItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
    case R.id.menu_delete: 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     mDbHelper.deleteReminder(info.id); 
     fillData(); // populate 
     return true; 
    } 
    return super.onContextItemSelected(item); 
} 

ReminderManager:ここ

は、私のコードの一部です.java

public class ReminderManager { 

private Context mContext; 
private AlarmManager mAlarmManager; 

public ReminderManager(Context context) { 
    mContext = context; 
    mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
} 

public void setReminder(Long taskId, Calendar when) { 

    Intent i = new Intent(mContext, OnAlarmReceiver.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId); 

    PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); 

    mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), 60 * 1000, pi); 
} 
} 

ReminderService.java

public class ReminderService extends WakeReminderIntentService { 

public ReminderService() { 
    super("ReminderService"); 
     } 

@Override 
void doReminderWork(Intent intent) { 
    Log.d("ReminderService", "Doing work."); 
    Long rowId = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID); 

    NotificationManager mgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(this, ReminderEditActivity.class); 
    notificationIntent.putExtra(RemindersDbAdapter.KEY_ROWID, rowId); 

    PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT); 

    Notification note=new Notification(android.R.drawable.stat_sys_warning, getString(R.string.notify_new_task_message), System.currentTimeMillis()); 
    note.setLatestEventInfo(this, getString(R.string.notify_new_task_title), getString(R.string.notify_new_task_message), pi); 
    note.defaults |= Notification.DEFAULT_SOUND; 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    int id = (int)((long)rowId); 
    mgr.notify(id, note); 


} 
} 

OnAlarmReceiver.java

public class OnAlarmReceiver extends BroadcastReceiver { 

private static final String TAG = ComponentInfo.class.getCanonicalName(); 


@Override 
public void onReceive(Context context, Intent intent) { 
    Log.d(TAG, "Received wake up from alarm manager."); 

    long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID); 

    WakeReminderIntentService.acquireStaticLock(context); 

    Intent i = new Intent(context, ReminderService.class); 
    i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid); 
    context.startService(i); 

} 
} 

EDIT

アラームを解除していない私の場合は問題私もでリマインダーを削除すると、なぜ、私は思ったんだけど、私の彼らはちょうど外出し続けています。

+0

[この繰り返しアラームをキャンセルするにはどうすればいいですか?](http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) –

+0

次にどこでアラームをキャンセルしますか?彼らはあなたのアプリのデータベースのエントリを削除するだけで停止するつもりはありません。 –

+0

@Mike M、ありがとうございました。あなたは正しいです、私は私のアラームをキャンセルしなければなりませんでした。 – pablo

答えて

0

アラームをキャンセルするためにsetRepeatingに電話するときは、PendingIntentと全く同じにAlarmManager.cancel(PendingIntent)と電話をかけなければなりません。

0

アラームを繰り返すために作成したpendingIntentをキャンセルする必要があります。あなたが設定で使用したのと同じ保留中のイベントを提供していることを確認します

AlarmManager.cancel(pendingIntent)を実行しますbuttonclickのようなコードを記述します。そうでなければ、供給されたものと一致する他のpendingIntentは取り消されます。

関連する問題