我尝试将此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();
}
});
编译器的错误如下图所示:
答案 0 :(得分:7)
您需要使用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);