
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!");}解决方法 单击 *** 作按钮时无法直接调用方法. 您必须使用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,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(){ }} 在Manifest中声明广播接收器
<receiver androID:name=".ActionReceiver" />
希望能帮助到你.
总结以上是内存溢出为你收集整理的Android – 从通知 *** 作按钮调用方法全部内容,希望文章能够帮你解决Android – 从通知 *** 作按钮调用方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)