Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Calling methods from notification action button

I know that you can launch Activities from the action buttons using PendingIntents. How do you make it so that the a method gets called when the user clicks the notification action button?

public static void createNotif(Context context){     ...     drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context)             .setSmallIcon(R.drawable.steeringwheel)             .setContentTitle("NoTextZone")             .setContentText("Driving mode it ON!")             //Using this action button I would like to call logTest             .addAction(R.drawable.smallmanwalking, "Turn OFF driving mode", null)             .setOngoing(true);     ... }  public static void logTest(){     Log.d("Action Button", "Action Button Worked!"); } 
like image 898
Faizan Syed Avatar asked Dec 24 '16 11:12

Faizan Syed


People also ask

How do I use NotificationListenerService?

To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.

Which API do you use to show a notification in the notification drawer and in the device's status bar?

Android 8.0, API level 26 Apps with active notifications display a notification "badge" on top of their app icon on the home/launcher screen. Users can now snooze a notification from the drawer.


1 Answers

You can't directly call methods when you click action buttons.

You have to use PendingIntent with BroadcastReceiver or Service to perform this. Here is an example of PendingIntent with BroadcastReciever.

First lets build a Notification

public static void createNotif(Context context){      ...     //This is the intent of PendingIntent     Intent intentAction = new Intent(context,ActionReceiver.class);      //This is optional if you have more than one buttons and want to differentiate between two     intentAction.putExtra("action","actionName");      pIntentlogin = PendingIntent.getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT);     drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context)             .setSmallIcon(R.drawable.steeringwheel)             .setContentTitle("NoTextZone")             .setContentText("Driving mode it ON!")             //Using this action button I would like to call logTest             .addAction(R.drawable.smallmanwalking, "Turn OFF driving mode", pIntentlogin)             .setOngoing(true);     ...  } 

Now the receiver which will receive this Intent

public class ActionReceiver extends BroadcastReceiver {      @Override     public void onReceive(Context context, Intent intent) {          //Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();          String action=intent.getStringExtra("action");         if(action.equals("action1")){             performAction1();         }         else if(action.equals("action2")){             performAction2();          }         //This is used to close the notification tray         Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);         context.sendBroadcast(it);     }      public void performAction1(){      }      public void performAction2(){      }  } 

Declare Broadcast Receiver in Manifest

<receiver android:name=".ActionReceiver" /> 

Hope it helps.

like image 114
Prince Bansal Avatar answered Sep 21 '22 19:09

Prince Bansal