2016-09-09 16 views
0

AlarmManagerを使用している通知が特定の日付に設定されているとします。表示される前にボタンの押下でその通知を削除したい場合はどうすればいいですか?私が間違っていない場合、NotificationManager.cancel(id)メソッドは、現在表示されている通知をキャンセルします。表示される前に削除したい場合はどうすればよいですか? arraylistのアイテムを削除する方法、データベース内の行を削除する方法など。Android - 通知が表示される前に削除またはキャンセルする


例私はid,namePersonオブジェクトを持っており、毎回、私はデータベースにPersonを追加し、Notificationは、指定した日付に設定されます。ここで

は、指定した日付のカレンダーのインスタンスと人物オブジェクトと呼ばれる一連の通知方法です。

public void setNotification(Calendar calendar,Person person){ 
    AlarmManager alertManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    Intent intent = new Intent(); 
    intent.setAction("android.media.action.DISPLAY_NOTIFICATION"); 
    intent.addCategory("android.intent.category.DEFAULT"); 
    intent.putExtra("myTag",person); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, person.getId(), intent, PendingIntent.FLAG_ONE_SHOT); 
    alertManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
} 

マイ警報受信機クラス(はい、私はDAOデザインパターンを実装しています私が持っています後者はシングルトンクラスであることとPersonDaoとPersonDaoImpl):アラームレシーバに設定

public class AlarmReceiver extends BroadcastReceiver { 
final static String GROUP_KEY = "GROUP_KEY"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    //retrieve info: 
    PersonDAO personDao = PersonDAOImpl.getInstance(context); 
    Person person = (Person)intent.getSerializableExtra("myTag"); 
    String name = person.getName(); 
    long[] vibrationPattern = {0, 300, 0, 300}; 

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(ViewDetails.class); 
    stackBuilder.addNextIntent(notificationIntent); 

    int requestCode = person.getId(); 
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_UPDATE_CURRENT); 
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    Notification notification = builder.setContentTitle("Deadline due today") 
      .setContentText("name: " + name) 
      .setTicker(name+"'s debt is due today") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setPriority(Notification.PRIORITY_MAX) 

      .setContentIntent(pi) 
      .build(); 

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(requestCode, notification); 


} 

例の日付は2016年9月12日午後3時でした。現在の日付はSeptermber 10 2016です。私がしたいのは、データベース内の人物を削除するときです。通知は削除/キャンセルされます。したがって、2016年9月12日午後3時には警告通知はありません。

+2

「通知は特定の日付に設定されました」 - これは「通知」の機能ではありません。あなたはどのようにこれをしようとしているのかを示す[mcve]を提供したいかもしれません。 – CommonsWare

+0

アラームマネージャを使用@CommonsWare – Andre

+0

@Andreあなたのコードの完全な例を投稿してください。 – mixel

答えて

1

あなたがAlarmManagerを経由して、スケジュールに何かをキャンセルしたい場合は、あなたが最初の場所で作業をスケジュールするために使用したものと同等のPendingIntentに渡し、AlarmManagercancel()を呼び出します。ここで、 "同等PendingIntent" によって、私は意味:

  • 同じ操作(例えば、getActivity()getService()getBroadcast()
  • 同じ要求コード(これらのメソッドへの2番目のパラメータ)
  • 同等"同等Intent" とIntent

、私は意味:

  • 同じコンポーネント
  • 同じアクション
  • 同じMIMEタイプ
  • 同じカテゴリの設定が元に設定していたこれらのプロパティの何のために
  • 同じデータ(Uri

元のPendingIntentIntent

+0

今すぐコードで質問を参照してください:) – Andre

+0

しかし、私はあなたの答えを理解しています:)私は目を覚ますときに私はあなたが最初に眠るよ挑戦しますよ – Andre

+0

それは動作します。ありがとう – Andre

関連する問題