我试图将这个onClick
侦听器放在服务中,以便在通知栏内的自定义button
视图中注册来自xml
的单击。但是onClickListener
得到了编译器错误。有人知道为什么会有编译错误吗?
RemoteViews remoteviews = new RemoteViews("com.example.test", R.layout.custom_notifications);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(AudioService.this)
.setContent(remoteviews)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setOngoing(true);
remoteviews.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(AudioViewer.this, "play button was clicked", Toast.LENGTH_LONG).show();
}
});
编译器的错误如下图所示:
最佳答案
您需要改用setOnClickPendingIntent
。RemoteViews类没有定义setOnClickListener
方法。
你需要这样做:
this.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(AudioViewer.this,
"play button was clicked", Toast.LENGTH_LONG).show();
}
}, new IntentFilter("MyRemoteViewsBroadcast"));
PendingIntent pi = PendingIntent.getBroadcast(this, 0,
new Intent("MyRemoteViewsBroadcast"), 0);
remoteviews.setOnClickPendingIntent(R.layout.custom_notifications, pi);