Search code examples
androidbuttonnotificationsremoteviewandroid-remoteview

Clickable buttons in notification using RemoteViews and service


I'm trying to make a music player that works on api level 8 but has the same notification feature as the standard music player i.e. 3 buttons which control the player. This is my code, i know it isn't really finished but for now i just want to figure out how to make the buttons clickable. If i try to click any buttons it just ignores them and is as if i clicked the whole notification.

    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.not_rview);

    views.setTextViewText(R.id.textView1, nowPlaying.title);


    Intent intentP1 = new Intent(this, MainActivity.class);
    intent.setAction("YOUR_AWESOME_ACTION");
    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 0, intentP1, 0);
    views.setOnClickPendingIntent(R.id.button1, pendingIntent1);

    Intent intentP2 = new Intent(this, MainActivity.class);
    intent.setAction("YOUR_AWESOME_ACTION");
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(this, 0, intentP2, 0);
    views.setOnClickPendingIntent(R.id.button2, pendingIntent2);

    Intent intentP3 = new Intent(this, MainActivity.class);
    intent.setAction("YOUR_AWESOME_ACTION");
    PendingIntent pendingIntent3 = PendingIntent.getBroadcast(this, 0, intentP3, 0);
    views.setOnClickPendingIntent(R.id.button3, pendingIntent3);


    Notification status = new Notification();
    status.contentView = views;
    status.flags |= Notification.FLAG_ONGOING_EVENT;
    status.icon = R.drawable.play;
    status.contentIntent = PendingIntent.getActivity(this, 0, new Intent(
            "MusicService")
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
    startForeground(1, status);

Also, is this the right direction i'm heading or should i do something different? I copied some(most) of the code from the built in app and some from other answers here.


Solution

  • I looked around for answers and it's not possible to do what i wanted. Apparently the default music player notification is specific to each phone and can't be accessed by 3rd party apps.