【问题标题】:Receive notification every five minutes and stop when click on it每五分钟收到一次通知,点击后停止
【发布时间】:2014-07-23 22:30:25
【问题描述】:

我想每 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);

这是我的 BroadcastReceiver 的代码:

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());

    }

【问题讨论】:

    标签: android broadcastreceiver android-notifications


    【解决方案1】:

    在你的通知中添加一个待处理的意图并让这个意图停止接收器。

        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");
    

    在 ActivityToStopReciever.class 中查看此链接以取消通知

    http://developer.android.com/reference/android/app/NotificationManager.html#cancel(int

    并在这里寻找解除警报

    Android AlarmManager problem with setting & resetting an alarm

    【讨论】:

    • 所以我必须创建一个新活动,在该活动中我取消通知。但是我怎样才能得到通知的 ID 呢?
    • 你在通知的时候设置了id。
    猜你喜欢
    • 1970-01-01
    • 2018-06-12
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2011-12-22
    • 1970-01-01
    相关资源
    最近更新 更多