我如何给广播接收器中的 pendingIntent 布尔状态成功或不成功

How do i give boolean status success or not to pendingIntent in broadcast Receiver

我正在开发来自 iBeacon 的 addAction 通知,我如何从这个 pendingIntent 通知中给这个 actionIntent 布尔状态成功或不成功,它将在 onReceive 方法中收到 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);
    }

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

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

非常感谢你们的帮助..

您尝试过使用 public static boolean 吗?您能准确解释一下您希望布尔值何时为 truefalse 吗?

在你的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);