Android-从通知操作按钮调用方法
我知道您可以使用PendingEvents从操作按钮启动活动。如何使a方法在用户单击通知操作按钮时被调用Android-从通知操作按钮调用方法,android,android-layout,android-notifications,action-button,Android,Android Layout,Android Notifications,Action Button,我知道您可以使用PendingEvents从操作按钮启动活动。如何使a方法在用户单击通知操作按钮时被调用 public static void createNotif(Context context){ ... drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.steeringwhe
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!");
}
单击操作按钮时不能直接调用方法 您必须将PendingEvent与BroadcastReceiver或Service一起使用才能执行此操作。下面是一个带有广播接收器的PendingEvent示例 首先让我们构建一个通知
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);
...
}
现在是接收此意向的接收者
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(){
}
}
<receiver android:name=".ActionReceiver" />
在清单中声明广播接收器
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(){
}
}
<receiver android:name=".ActionReceiver" />
希望有帮助。谢谢,这很有道理。然而,我有点困惑,为什么您将
intentAction
传递到addAction
。是否更新了pIntentlogin
。我的错!:)非常感谢你!这应该是Android开发者指南的教程。这是一个很好的答案,但有一部分需要警告;在意图中添加一个额外的元素会导致进一步的混淆:“一个常见的错误:创建多个PendingEvent对象,其意图仅在“额外”内容中有所不同,期望每次都获得不同的PendingEvent。这不会发生。如果您使用两个根据{@link intent#filterEquals等效的意图对象(Intent)Intent.filterEquals},那么您将为它们获得相同的挂起内容。有两种典型的方法来处理此问题…”