android 是否可以从通知操作发送LocalBroadcast?

1mrurvl1  于 2023-01-15  发布在  Android
关注(0)|答案(2)|浏览(105)

我想在点击通知内的按钮时发送LocalBroadcast。我知道如何使用常规广播发送LocalBroadcast,但我想将广播保留在我的应用程序内。可以吗?
我的代码大致如下:

NotificationCompat.Builder notificationBuilder  = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("content")
    .setSmallIcon(R.drawable.icon1)
    .setContentIntent(pIntent)
    .setAutoCancel(true);

Intent myButtonIntent = new Intent(BUTTON_PRESSED);

// the following line gives me a normal broadcast, not a LocalBroadcast
PendingIntent myButtonpIntent = PendingIntent.getBroadcast(context, 12345, myButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
notificationBuilder.addAction(R.drawable.icon2, "OK", myButtonpIntent);
Notification notification = notificationBuilder.build();
NotificationManager notificationManager = 
    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

并且:

BroadcastReceiver bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(BUTTON_PRESSED)) {
            // do something
        }
    }
};

LocalBroadcastManager bManager = LocalBroadcastManager.getInstance(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BUTTON_PRESSED);
bManager.registerReceiver(bReceiver, intentFilter); // I want to use the LocalBroadcastManager
// registerReceiver(bReceiver, intentFilter);  // Instead, I have to use this line for a non-local broadcast
xriantvc

xriantvc1#

没有。
首先,LocalBroadcastManager是支持包(添加到应用程序中的 * 可选 * 库)的一部分,而不是系统本身的一部分。
其次,即使是这样,通知服务也是由所有应用程序使用的,因为它是一个共享资源,所以当你发布通知时不会发生任何“本地”的事情。

pod7payv

pod7payv2#

这可能有用(对我有用)

将您的BroadcastReceiver添加到manifest.xml

<!-- If this receiver listens for broadcasts sent from the system or from
         other apps, even other apps that you own, set android:exported to "true". -->
    <receiver android:name=".myBroadcastReceiver" android:exported="false">
        <intent-filter>
            <action android:name="some" />
        </intent-filter>
    </receiver>

创建通知和添加操作(.addaction())并将广播发送到BroadcastReceiver
(you可以使用辅助功能)

Intent Rintent = new Intent(this , YOUR_BroadcastReceiver_CLASS.class );

  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
  notificationIntent, 0);
  createNotificationChannel(ChanelID ,"name " , "Desc" );
  startForeground(FLAG_FORGRANDONLLINE, new NotificationCompat.Builder(ServiceFindTask.this,
  NotifID) // don't forget create a notification channel first
  .setOngoing(true)
  .setSmallIcon(R.mipmap.somet)
  .setContentTitle(getString(R.string.app_name))
  .setContentText("Service is running ")
  .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
  .setVibrate(new long[]{1000,100})
  .setContentIntent(pendingIntent)
  .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
  .setCustomContentView(notificationLayout)

  // Sending a action for BroadcastReceiver class
  .addAction(R.drawable.body,"GO offline" , makePendingIntent(SERVICE_TO_CLIENT_GO_OFFLINE , null , Rintent))
  .build());

   // Helper function

    public PendingIntent makePendingIntent(Integer action , @Nullable String data ,Intent intent) {
    intent.setAction( action.toString());
    if(data != null){
    intent.putExtra("kay", data);}
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    return pendingIntent;
 }

接收操作并根据通知操作使用LocalBroadcastManager创建LocalBroadcast

public class myBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      switch (Integer.parseInt(action)){
        case Service.SERVICE_TO_CLIENT_GO_OFFLINE:
          helpLocalBroadcastManager(intent , context , Service.SERVICE_TO_CLIENT_GO_OFFLINE , null);
          break;
  
      }
  
    }
    private void helpLocalBroadcastManager(Intent intent ,Context context ,Integer action , @Nullable String data ) {
  
  
       intent = new Intent(action.toString());
      // Adding some data
      if(data != null){
        intent.putExtra("kay", data);}
      LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
    
  }

现在,您可以在连接到BroadcastReceiver的任何活动中接收LocalBroadcast,如下所示:

private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
   @Override
   public void onReceive(Context context, Intent intent) {
     String action = intent.getAction();
     switch (Integer.parseInt(action)){
       case Service.SERVICE_TO_CLIENT_GO_OFFLINE:
         Toast.makeText(Activity_Main.this , "This is massage from Activity_Main " , Toast.LENGTH_SHORT).show();
         break;
         }
 }
};
   @Override
 public void onResume() {
   super.onResume();
   LocalBroadcastManager.getInstance(this)
     .registerReceiver(messageReceiver, new IntentFilter(String.valueOf(ServiceFindTask.SERVICE_TO_CLIENT_GO_OFFLINE)));
}

这个问题是8年前的,但这是我现在需要的,这是我如何做到的,谢谢阅读

相关问题