【问题标题】:How to cancel notification of BroadcastReceiver如何取消广播接收器的通知
【发布时间】:2016-05-16 14:19:03
【问题描述】:

我的 AlertReceiver 类正在创建通知服务。如何取消已创建的通知?

这是我的代码:

public class AlertReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
       createNotification(context,"Title","This is message body", "Alert");
    }
    public void createNotification(Context context,String msg,String msgText, String  msgAlert){
        PendingIntent notificIntent=PendingIntent.getActivity(context, 0,
                new Intent(context,MainActivity.class),0);
        NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.my_image)
                .setContentTitle(msg)
                .setTicker(msgAlert)
                .setContentText(msgText);
        mBuilder.setContentIntent(notificIntent);
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        NotificationManager mNotificationManager=
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1,mBuilder.build());

    }
}

【问题讨论】:

  • 我的意思是如何为这个类编写取消方法...

标签: android notifications broadcastreceiver


【解决方案1】:

使用 unregisterReceiver(BroadcastReceiver receiver) 作为单独的函数从您的类中注销广播接收器类。

public void unRegisterAlertreceiver(){
     unregisterReceiver(this);
}

【讨论】:

  • 我会在课堂之外创建单独的广播接收器实例。
  • 你能写一个示例代码吗?真的很有帮助
  • 公共类 AlertActivity 扩展 Activity { private final AlertReceiver myalertreceiver = new AlertReceiver();公共无效 onDestroy() { unregisterReceiver(myalertreceiver); } }
  • 看看这个链接可能会有所帮助。 stackoverflow.com/questions/7439041/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多