【问题标题】:Android - Calling methods from notification action buttonAndroid - 从通知操作按钮调用方法
【发布时间】:2016-12-24 11:01:35
【问题描述】:

我知道您可以使用 PendingIntents 从操作按钮启动活动。当用户单击通知操作按钮时,如何实现调用 a 方法?

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!");
}

【问题讨论】:

    标签: android android-layout android-notifications action-button


    【解决方案1】:

    单击操作按钮时不能直接调用方法。

    您必须将 PendingIntent 与 BroadcastReceiver 或 Service 一起使用才能执行此操作。这是一个带有 BroadcastReciever 的 PendingIntent 示例。

    首先让我们构建一个通知

    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);
        ...
    
    }
    

    现在接收这个 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(){
    
        }
    
    }
    

    在清单中声明广播接收器

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

    希望对你有帮助。

    【讨论】:

    • 谢谢,这很有意义。但是,我对您为什么将intentAction 传递给addAction 感到有些困惑。会是pIntentlogin吗?
    • 已更新。我的错! :)
    • 非常感谢!
    • 这应该是Android开发者指南的教程。
    • 这是一个很好的答案,但有一部分需要警告;向意图添加额外内容将导致进一步的混乱:“一个常见的错误:创建多个 PendingIntent 对象,其意图仅在其“额外”内容上有所不同,期望每次都获得不同的 PendingIntent。这不会 发生。如果你使用两个根据 {@link Intent#filterEquals(Intent) Intent.filterEquals} 等价的 Intent 对象,那么你将得到相同的 PendingIntent 为他们两个。有两种典型的处理方式有了这个……”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多