所以,经过一番绞尽脑汁之后,我已经无计可施了。我有一个媒体播放器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);
and my 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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)