gpt4 book ai didi

android - 我如何给广播接收器中的 pendingIntent bool 状态成功或不成功

转载 作者:行者123 更新时间:2023-11-29 23:48:01 25 4
gpt4 key购买 nike

我正在开发来自 iBeacon 的 addAction 通知,如何从将在 onReceive 中收到的 pendingIntent 通知中为该 actionIntent 提供 bool 状态成功或不成功 ActionReceiver.class 中的方法这是来自 MainActivity.class

的通知方法
Intent broadcastIntent = new Intent(this, ActionReceiver.class);
broadcastIntent.putExtra("action", "notif1");
PendingIntent actionIntent = PendingIntent.getBroadcast(
this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.th_notif_logo)
.setTicker("Your Title")
.setWhen(System.currentTimeMillis())
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.addAction(R.drawable.ic_petunjuk_icon,"Clue", actionIntent)
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setStyle(new Notification.BigTextStyle().bigText(message))
.setContentIntent(pIntentAction)
.setPriority(Notification.PRIORITY_HIGH)
.build();

这是ActionReceiver.class中的onReceive方法

@Override
public void onReceive(Context context, Intent intent) {
String action=intent.getStringExtra("action");
if(action.equals("notif1")){
SharedPreferences preferences = context.getSharedPreferences("MYPREFS", MODE_PRIVATE);
final String nama_tim = preferences.getString("username", "Key not correct");
InserData(nama_tim, "fsrd");
Toast.makeText(context.getApplicationContext(),"Action Receiver berhasil masuk",Toast.LENGTH_SHORT).show();
}
else if(action.equals("action2")){

}
//This is used to close the notification tray
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
}

所以当我得到 bool 状态成功与否时,我可以在 MainActivity.class

中做一些事情
    if (actionIntent == success){
//do something
}

非常感谢你们的帮助......

最佳答案

在你的MainActivity.class

Intent broadcastIntent = new Intent("action-key"); // Specify the action to your intent.
broadcastIntent.setPackage(getPackageName());

// The values you want receive in the BroadcastReceiver.
broadcastIntent.putExtra("key-action", "notif1");
broadcastIntent.putExtra("key-boolean", true);

PendingIntent actionIntent = PendingIntent.getBroadcast(
this, 0, broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT)
NotificationCompat.Action notificationAction = new NotificationCompat.Action(R.drawable.ic_action, "Action Name", actionIntent)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT);

Notification notification = new NotificationCompat.Builder(this,"channelId")
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Your Title")
.setWhen(System.currentTimeMillis())
.setContentTitle("test")
.setContentText("test")
.setAutoCancel(true)
.addAction(notificationAction) // Adds an action button to the notification
.setContentIntent(contentIntent) // Launches the intent when you click the notification.
.setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
.setPriority(Notification.PRIORITY_HIGH)
.build();
....

....
// Instead of using your own class receiver, you have to use the default default class receiver if you want your data to reach to your MainActivity.class easily.
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == "action") {
bool result = intent.getBooleanExtra("key-boolean", false); // Your value
}
}
};

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("action");
registerReceiver(broadcastReceiver, intentFilter);

关于android - 我如何给广播接收器中的 pendingIntent bool 状态成功或不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51169192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com