自定义通知

时间:2016-07-26 19:44:03

标签: android notifications broadcastreceiver custom-notificaiton

我试图在自定义通知的按钮上捕获点击操作。我想避免创建一个扩展BroadcastReciever的类。为此,我在服务类中编写如下代码:

this.registerReceiver(new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.v ("Myreceiver", "on click catch")
        }
    }, new IntentFilter("MyRemoteViewsBroadcast"));

PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent("MyRemoteViewsBroadcast"), 0);
remoteviews.setOnClickPendingIntent(R.I'd. Button, pi);

但它不起作用。我失去了什么吗?

编辑:使用"它不起作用"我的意思是当我点击按钮时从不调用OnReceive()方法。 (对不完整的问题抱歉)

1 个答案:

答案 0 :(得分:0)

我刚刚意识到问题出在哪里!!!

代码完美无缺!!!我在自己的代码中写道:

PendingIntent playPendingIntent = PendingIntent.**getService**( getApplicationContext(), NOTIFICATION_ID, new Intent("MyRemoteViewsBroadcast"), 0);

而不是:

PendingIntent playPendingIntent = PendingIntent.**getBroadcast**( getApplicationContext(), NOTIFICATION_ID, new Intent("MyRemoteViewsBroadcast"), 0);

愚蠢的错误。我需要休息!很抱歉给您带来不便。