Android-如何使用操作创建通知
我正在创建这样的通知Android-如何使用操作创建通知,android,android-notifications,Android,Android Notifications,我正在创建这样的通知 Notification.Builder builder = new Notification.Builder(context); builder.setContentTitle(notifyMessage1) .setContentText(notifyMessage2) .setSmallIcon(R.mipmap.ic_launcher); Notification notificati
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle(notifyMessage1)
.setContentText(notifyMessage2)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = builder.build();
我想在通知中添加一个操作
builder.addAction();
实现addAction(图标、标题、挂起内容)代码>已弃用
我的geal是创建一个没有图标的通知动作,我该如何实现呢 使用NotificationCompat而不是像这样的通知:
Notification.Action action = new NotificationCompat.Action(icon, title, pendingIntent);
Notification notification = new NotificationCompat.Builder(context)
.addAction(action)
.build();
单击操作按钮时不能直接调用方法
您需要将PendingEvent与BroadcastReceiver或Service一起使用才能执行此操作
下面是广播接收器的未决意图示例
首先生成通知
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"></receiver>
不要忘记在清单中声明广播接收器
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"></receiver>
希望它能帮助您。可能会有帮助。好吧,但我不想要图标。如果您不需要图标,我想您可以通过0
。我不确定。NotificationCompat不是也被弃用了吗?@braulio.cassule不是。它现在被弃用了。android.support.v4.app.NotificationCompat已被弃用,但androidx.core.app.NotificationCompat未被弃用,并且在当前文档中被广泛使用。在将数据添加到清单文件中之前,我一直无法让本地广播公司接收数据