0

我试图捕获自定义通知按钮上的单击操作。我想避免创建一个类延伸BroadcastReciever。要做到这一点,我在我的服务类内编码如下:自定义通知中的onClick按钮

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

你是什么意思的“不工作”到底是什么问题? – ChrisStillwell

+0

感谢您的快速回答!我的意思是,当我点击时,onReceive()方法没有被调用,所以Log不会出现... –

回答

0

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

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

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

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

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