在点击监听器通知远程视窗

在点击监听器通知远程视窗

本文介绍了在点击监听器通知远程视窗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,很多的头后抓,我在我束手无策。我有一个媒体播放器 RemoteViews 在我通知,我想是能够访问播放,暂停,previous和下一个按钮。

So, after a lot of head scratching, I am at my wit's end. I have a media player RemoteViews in my notification and I would like to be able to access the play, pause, previous and next buttons.

我知道 setOnClickPendingIntent()将被用于从通知通信。不过,我在想如何将工作。

I know that setOnClickPendingIntent() will be used to communicate from the notification. However, I am left wondering how that will work.

是否有可能让服务办理点击?

Is it possible to let the service handle the clicks?

我已经试过这一点,但徒劳无功。我试图让我的服务处理玩家的暂停和恢复:

I have tried this, but in vain. I was trying to let me service handle the pause and resume of the player:

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 为空。如果有差别。

and my IBinder is null. If that makes a difference.

如何暂停,并从通知?播放音乐

推荐答案

根据 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);

这篇关于在点击监听器通知远程视窗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:04