我是android编码的初学者 . 我正在开展媒体播放器项目作为学习练习 . 我已设法让媒体播放器在后台播放,并在活动被推送到后台时显示播放/暂停按钮的通知 .

现在我已经在我的活动上创建了onNewIntent()来接收来自通知面板的动作 . 我能够播放和暂停音乐,但是在通知上单击这些按钮时,它会打开播放器活动 . 我不知道如何在不打开活动的情况下仅执行播放或暂停操作 .

我的活动:

@Override
protected void onNewIntent(Intent intent) {        
    super.onNewIntent(intent);
    setIntent(intent);        
    if (intent.getExtras() != null) {
        String tmp;
        tmp = intent.getExtras().getString("DO");
        if (tmp != null) {
            if (tmp.equals("pause")) {                    
                pausePlaying();
            }else if(tmp.equals("play")){
                startPlaying();
            }
            else if (tmp.equals("exit")) {
                Log.i("onNewIntent", "exit");
                finish();
            }
        }
    }
}

NotificationPanel.java(我在哪里设置pendingIntents)

public class NotificationPanel{
    private Context parent;
    private NotificationManager nManager;
    private android.support.v4.app.NotificationCompat.Builder nBuilder;
    private RemoteViews remoteView;

    public NotificationPanel(Context parent) {
        // TODO Auto-generated constructor stub
        this.parent = parent;
        nBuilder = new android.support.v7.app.NotificationCompat.Builder(parent)
                .setContentTitle("Transistor")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setOngoing(true);

        remoteView = new RemoteViews(parent.getPackageName(), R.layout.activity_notification_return_slot);

        //set the button listeners
        setListeners(remoteView);
        nBuilder.setContent(remoteView);

        nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
        nManager.notify(2, nBuilder.build());
    }

    public void setListeners(RemoteViews view){
        //listener 1
        Intent pauseIntent = new Intent(parent,ChannelActivity.class);
        pauseIntent.putExtra("DO", "pause");
        pauseIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        pauseIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        pauseIntent.setAction(Intent.ACTION_MAIN);
        pauseIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent btn1 = PendingIntent.getActivity(parent, 0, pauseIntent, 0);
        view.setOnClickPendingIntent(R.id.btn1, btn1);

        ...
        //Similarly Intents are set for other actions 
    }
}

我搜索了很长时间才找到解决方案 . 找不到一个 . 可能是我没有正确搜索 . 任何有关这方面的帮助将不胜感激 . TIA .