5

所以,经过很多挠头后,我无计可施。我的通知中有一个媒体播放器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的为空。如果这有所作为。

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

4

1 回答 1

12

根据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);
于 2014-08-24T19:30:07.593 回答