Android-通知操作按钮中的调用方法 [英] Android - Calling methods from notification action button

查看:169
本文介绍了Android-通知操作按钮中的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道您可以使用PendingIntents从操作按钮启动活动".您如何做到这一点,以便在用户单击通知操作按钮时调用a方法?

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

推荐答案

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

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

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

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

首先让我们构建一个通知

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" />

希望有帮助.

这篇关于Android-通知操作按钮中的调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆