Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 每五分钟接收一次通知,并在单击时停止_Android_Broadcastreceiver_Android Notifications - Fatal编程技术网

Android 每五分钟接收一次通知,并在单击时停止

Android 每五分钟接收一次通知,并在单击时停止,android,broadcastreceiver,android-notifications,Android,Broadcastreceiver,Android Notifications,我想每隔5分钟告诉用户他有事情要做。我每5分钟收到一次通知。但我的问题是我不知道在哪里可以阻止它。(我想在用户单击通知时停止它) 以下是我活动中的代码: Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, heure); cal.add(Calendar.MINUTE, minute); Intent intent = new Intent(this,

我想每隔5分钟告诉用户他有事情要做。我每5分钟收到一次通知。但我的问题是我不知道在哪里可以阻止它。(我想在用户单击通知时停止它)

以下是我活动中的代码:

            Calendar cal = Calendar.getInstance();
        cal.add(Calendar.HOUR, heure);
        cal.add(Calendar.MINUTE, minute);

        Intent intent = new Intent(this, MyBroadcastReceiver.class);

        PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager) this
                .getSystemService(Context.ALARM_SERVICE);

        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), minute*60*1000+heure*3600*1000,  sender);
这是我的广播接收器的代码:

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.d("BroadcastReceiver", "debut receive");

    Intent resultIntent = new Intent(context,
            PiecesPasEnvoyesActivity.class);

    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context,
            0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Messages à envoyer")
    .setContentText("Vous avez des messages à envoyer");

    mBuilder.setAutoCancel(true);
    mBuilder.setPriority(Notification.PRIORITY_MAX);
    mBuilder.setContentIntent(resultPendingIntent);

    int mNotificationId = 001;

    NotificationManager mNotifyMgr = (NotificationManager) context
            .getSystemService(Application.NOTIFICATION_SERVICE);


    mNotifyMgr.notify(mNotificationId, mBuilder.build());

    }

在通知中添加一个挂起的意图,并让该意图停止接收者

    Intent notificationIntent = new Intent(this, ActivityToStopReciever.class);

    notificationIntent.setFlags(Notification.FLAG_AUTO_CANCEL);
    PendingIntent intent = PendingIntent.getActivity(getApplicationContext(), 0,
                    notificationIntent, 0);

Notification.Builder mBuilder = new Notification.Builder(context)
    .setSmallIcon(R.drawable.courier_blanc)
    .setContentTitle("Messages à envoyer")
    .setContentIntent(intent)
    .setContentText("Vous avez des messages à envoyer");
在ActivityToStopReceiver.class中,查看此链接以取消通知

看看这里有没有解除警报


因此我必须创建一个新的活动,在其中取消通知。但是我怎样才能获得通知的ID呢?您在发出通知时设置ID。