1

I am a beginner in android coding. I am working on a mediaplayer project as a learning exercise. I have managed to get the mediaplayer playing in the background and to show a notification with play/pause button when the activity is pushed to background.

Now I have created onNewIntent() on my activity to receive the action from the notification panel. I am able to play and pause the music but on click of these button on notification, it opens the player activity. I don't know how to perform only the play or pause action without opening the activity.

My activity:

@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 (Where I set 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 
    }
}

I searched for a long time for a solution. Couldn't find one. May be I didn't search correctly. Any help on this would be appreciated. TIA.

Ganesh
  • 11
  • 2
  • As an exercise: You can have a local `BroadcastReceiver` inside your activity and play/pause music there. Use `getBroadcast()` instead of `getActivity()` in your pending intent. Also, for music you may use a foreground service. – Shaishav Aug 20 '16 at 20:24

0 Answers0