待处理的BroadCastReceiver无法开始使用setDeleteIntnet [英] Pending BroadCastReceiver does not start using setDeleteIntnet

本文介绍了待处理的BroadCastReceiver无法开始使用setDeleteIntnet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发出通知,而我想做的是,当用户单击我发送的通知以清除它时,我希望执行待处理的广播.有一种称为"setDeleteIntent"的方法.根据此方法的文档,当用户清除收到的通知时,它应启动待处理的BraodCast接收器.7

I make a notification and what i want to do is, when the user clicks the notification I sent to clear it, i want a pending broadcast to be executed. there is a method called 'setDeleteIntent'. According to the documentation of this method, it should starts a pending BraodCast Receiver when the user clear the notification received.7

我按下面的代码所示创建一个Broadcast Receiver类,并为'setDeleteIntent'方法提供未决的广播.但是在运行时,当接收到的通知随后被清除时,BroadcastReceiver的"onReceive"中的日志消息未显示在控制台上!

I create a Broadcast Receiver class as shown below in the code, and I supply the the 'setDeleteIntent' method with pending broadcast. But at run time, when the notification received then cleared, the Log message in 'onReceive' of the BroadcastReceiver did not show on the console!!

请看下面的代码,请让em k知道为什么broadcastReceiver的'onReceive'永远不会执行.

please have a look at the code below, and please let em k know why the 'onReceive' of the broadcastReceiver never executes.

注释:如代码所示,使用"LocalBroadCastReceiver"注册了BroadCasrReceiver.

note: the BroadCasrReceiver was registered using 'LocalBroadCastReceiver' as shown in the code,.

代码:

    String title = getString(R.string.app_name);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setContentTitle(title);
    builder.setContentIntent(contentIntent).setAutoCancel(true);

    //builder.setWhen(Long.valueOf(eu.man.m24wsapp.utils.gcm.TimeUtils.getNowTS()) + 5000);
    builder.setLights(Color.argb(1, 0, 0, 255), 1000, 400);
    builder.setOngoing(false);
    builder.setAutoCancel(true);


    builder.setDeleteIntent(getPendingBroadCast());
    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, builder.build());
    ....
    ....
    ....

    //getPendingBroadCast method
    private PendingIntent getPendingBroadCast() {
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(new BCRDismissNotification(),
            new IntentFilter(BCRDismissNotification.ACTION_DISMISSED_NOTIFICATION));

    Intent intent = new Intent(this, BCRDismissNotification.class);
    //intent.setAction(BCRDismissNotification.ACTION_DISMISSED_NOTIFICATION);
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

BroadCast接收器:

public class BCRDismissNotification extends BroadcastReceiver {
private final static String TAG = BCRDismissNotification.class.getSimpleName();
public final static String ACTION_DISMISSED_NOTIFICATION = "action_dismissed_notification";

@Override
public void onReceive(Context context, Intent intent) {

    Log.i(TAG, "*********************   BCR     **************");
}

}

推荐答案

您已经回答了您自己的问题.这不适用于 LocalBroadcastManager

You have pretty much answered your own question. This can't work with LocalBroadcastManager!

当您使用 LocalBroadcastManager 注册接收器时,它仅侦听应用程序广播的 Intent 广播.对于您而言,广播 Intent 是由Android的通知框架发送的,它不是您的应用程序的一部分.在这种情况下,您的注册接收者将不会被触发.

When you register a receiver with using LocalBroadcastManager, it only listens for broadcast Intents that are broadcast by your application. In your case, the broadcast Intent is sent by Android's notification framework, which is not part of your application. In this case, your registered receiver will not get triggered.

您需要注册您的接收器才能进行全球广播.您可以通过创建接收者的实例并在应用程序 Context 上调用 registerReceiver()来执行此操作,也可以使用将接收者添加到清单中< receiver> 标记,并确保您设置 android:exported ="true"

You need to register your receiver for global broadcasts. You can do this either by creating an instance of your receiver and calling registerReceiver() on the application Context OR you can just add your receiver to the manifest with a <receiver> tag and make sure that you set android:exported="true"

这篇关于待处理的BroadCastReceiver无法开始使用setDeleteIntnet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆