4

我试图将此侦听器放在服务中,以在通知栏内的自定义视图中onClick注册点击。但是得到编译器错误。有谁知道为什么会出现编译错误?buttonxmlonClickListener

   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();
            }
        });

编译器的错误如下图所示:

在此处输入图像描述

4

1 回答 1

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);
于 2012-12-20T06:39:25.787 回答