如何使用从通知操作,而无需启动活动 [英] How to use Actions from Notification without starting Activity

查看:102
本文介绍了如何使用从通知操作,而无需启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作的一个简单的音乐播放器。音乐播放器,因为它的名字指出,可以播放一首歌曲,暂停播放,前进到下一曲,回到previous歌曲,并完全停止播放。当您播放一首歌曲,会有与艺术家姓名和歌曲名称显示通知;该通知还拥有三个按钮(动作):停止,暂停下一步。

So I'm working on a simple music player. The music player as it names states, can play a song, pause playback, forward to next song, go back to previous song, and stop playback completely. When you play a song, there will be a notification displayed with the Artist Name, and the Song Name; this notification also has three buttons (Actions): Stop, Pause an Next.

什么我在使用的是确保任何行动被点击时,播放控制相关的行动被触发,和好了,我完全不知道如何做的问题。我搜索了Android通知:<一href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html">http://developer.android.com/guide/topics/ui/notifiers/notifications.html但它并没有澄清,或者致力于在通知操作太多信息。

What I'm having issues with is making sure that when either action is clicked, the playback control related to the Action is triggered, and well, I have absolutely no idea on how to do that. I searched the Android Notification: http://developer.android.com/guide/topics/ui/notifiers/notifications.html but It does not clarifies, or dedicates too much info on Notification Actions.

下面是实例,一个简单的动作(这应该与下一步按钮,点击通知相关联:注意:所有的code说明是根据以下包写入 com.deadpixels.light.player 和活动被称为: PlayerActivity

Here is a simple action for instance (which should be associated with the "Next" button click on the notification: Note: All of the code described below is written under the following package: com.deadpixels.light.player and the Activity is called: PlayerActivity

public void nextSong() {
    if (position == songs.size() -1) {
        Toast.makeText(this, "No more songs on queue", Toast.LENGTH_SHORT).show();
        if(mPlayer.isPlaying()) {
            return;
        }
        else {
            buttonPlay.setImageResource(R.drawable.button_play);
        }
        return;
    }
    else {
        position++;
        playSong(songs.get(position));
    }
}

下面是我试图做的:

Intent nextIntent = new Intent(KEY_NEXT);
PendingIntent nextPendingIntent = createPendingResult(0, nextIntent, 0);

在哪里的NextIntent的动作是:

Where the action for the NextIntent is:

public static final String KEY_NEXT = "com.deadpixels.light.player.PlayerActivity.nextSong";

然后我再补充一点,以通知通过的addAction():

and then I add that to the Notification via addAction():

mBuilder.addAction(R.drawable.not_action_next, "Next", nextPendingIntent);

难道你们知道还有什么我可以试试吗?用我上面所解释什么事情都不做,该通知显示出来,并有三个操作按钮,但点击他们什么都不给我。

Do you guys know what else I could try? Using what I explained above does nothing at all, the notification shows up, and has three action buttons, but clicking on them does nothing for me.

在一个点上我想,也许,如果我说的意图过滤器与动作的名称,但转念一想,好了,他们都在同一个命名空间,为什么要我?

At one point I thought maybe if I added the intent filters with the action names, but then I thought, well, they are all on the same namespace, why should I?

推荐答案

我已经解决了使用广播这个问题。例如,发送广播的进入到下一首歌曲,你可以用下面的:

I've solved this problem using broadcasts. For example, to send a broadcast that advances to the next song, you can use the following:

Intent nextIntent = new Intent(KEY_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0, nextIntent, 0);

然后,注册的BroadcastReceiver 中的活动

IntentFilter filter = new IntentFilter();
filter.addAction(KEY_NEXT);
// Add other actions as needed

receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(KEY_NEXT)) {
            nextSong();
        } else if (...) {
            ...
        }
        ...
    }
}

registerReceiver(receiver, filter);

如果您使用的是服务管理后台播放,那么你要注册的的BroadcastReceiver 在服务,而不是活动。一定要保存接收实例的地方,这样你可以注销它时,活动或服务被关闭。

If you're using a service to manage background playback, then you'll want to register the BroadcastReceiver in the service instead of the activity. Be sure to store the receiver instance somewhere so that you can unregister it when the activity or service is shut down.

这篇关于如何使用从通知操作,而无需启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆