gpt4 book ai didi

android - 单击监听器上的通知 RemoteView

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:58:19 26 4
gpt4 key购买 nike

所以,经过大量的摸索之后,我已经束手无策了。我的通知中有一个媒体播放器 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);

关于android - 单击监听器上的通知 RemoteView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25475142/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com