如何使用从通知操作,而无需启动活动如何使用、操作、通知

由网友(本尊无赖)分享简介:所以我工作的一个简单的音乐播放器。音乐播放器,因为它的名字指出,可以播放一首歌曲,暂停播放,前进到下一曲,回到previous歌曲,并完全停止播放。当您播放一首歌曲,会有与艺术家姓名和歌曲名称显示通知;该通知还拥有三个按钮(动作):停止,暂停下一步。 So I'm working on a simple music...

所以我工作的一个简单的音乐播放器。音乐播放器,因为它的名字指出,可以播放一首歌曲,暂停播放,前进到下一曲,回到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通知: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.

阅读全文

相关推荐

最新文章