通知RemoteView点击listener



所以,在绞尽脑汁之后,我真是无计可施。我有一个媒体播放器RemoteViews在我的通知,我希望能够访问播放,暂停,上一个和下一个按钮。

我知道setOnClickPendingIntent()将被用来从通知进行通信。然而,我想知道这将如何工作。

是否可以让服务处理点击?

我试过了,但是没有用。我试图让我服务处理播放器的暂停和恢复:

rm = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notif_media_player);
        Intent intent = new Intent(getApplicationContext(), MusicService.class);
        intent.putExtra(REQUEST_CODE, PAUSE);
        PendingIntent pending = PendingIntent.getService(getApplicationContext(), PAUSE, intent, 0);
        rm.setPendingIntentTemplate(R.id.pause, pending);
        Notification notif =  new NotificationCompat.Builder(getApplicationContext())
                                 .setOngoing(true)
                                 .setSmallIcon(R.drawable.ic_launcher)
                                 .setContent(rm)
                                 .build();
        NotificationManager mgr = (NotificationManager) getApplicationContext()
                                  .getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(NOTIFICATION_ID, notif);  

,我的IBinder为空。如果有影响的话。

如何从通知中暂停和播放音乐?

根据CommonsWare的建议:

        rm = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notif_media_player);
        Intent pauseIntent = new Intent(getApplicationContext(), MusicService.class);
        Intent dismissIntent = new Intent(getApplicationContext(), MusicService.class);
        pauseIntent.putExtra(REQUEST_CODE, PAUSE);
        dismissIntent.putExtra(REQUEST_CODE, DISMISS);
        PendingIntent pause = PendingIntent.getService(getApplicationContext(), PAUSE, pauseIntent, 0);
        PendingIntent dismiss = PendingIntent.getService(getApplicationContext(), DISMISS, dismissIntent, 0);
        rm.setOnClickPendingIntent(R.id.pause, pause);
        rm.setOnClickPendingIntent(R.id.dismiss, dismiss);
        Notification notif =  new NotificationCompat.Builder(getApplicationContext())
                                 .setOngoing(true)
                                 .setSmallIcon(R.drawable.ic_launcher)
                                 .setContent(rm)
                                 .build();
        NotificationManager mgr = (NotificationManager) getApplicationContext()
                                  .getSystemService(Context.NOTIFICATION_SERVICE);
        mgr.notify(NOTIFICATION_ID, notif);

最新更新