Skip to content Skip to sidebar Skip to footer

Perform Action On Button Click In Custom Notification : Android

I am trying to perform some action like pause music , play music on button click of a custom notification in android. Currently I am doing it in this way , int icon = R.drawab

Solution 1:

First of All assign a intent to your buttons:

RemoteViewscontentView=newRemoteViews(context.getPackageName(), R.layout.player_notify_layout);
    IntentbuttonsIntent=newIntent(context, NotifyActivityHandler.class);
    buttonsIntent.putExtra("do_action", "play");
    contentView.setOnClickPendingIntent(R.id.imgPlayPause, PendingIntent.getActivity(context, 0, buttonsIntent, 0));

Then create an activity to handle every action that occurred by notification:

publicclassNotifyActivityHandlerextendsActivity {
           publicstaticfinalStringPERFORM_NOTIFICATION_BUTTON="perform_notification_button";
           
           @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);

               Stringaction= (String) getIntent().getExtras().get("do_action");
               if (action != null) {
                   if (action.equals("play")) {
                       // for example play a music
                   } elseif (action.equals("close")) {
                       // close current notification
                   }
               }

               finish();
         }
    }

Finally, you should define activity in AndroidManifest.xml. Also you can check this link.

Post a Comment for "Perform Action On Button Click In Custom Notification : Android"