繁体   English   中英

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

[英]Is it possible to send a LocalBroadcast from a notification action?

单击通知中的按钮时,我想发送 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

没有。

首先, LocalBroadcastManager是支持包(您添加到应用程序中的可选库)的一部分,而不是系统本身的一部分。

其次,即使有,通知服务也被所有应用程序使用。 由于它是共享资源,因此发布通知时不会发生“本地”事件。

这可能有用(对我有用)

将您的 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

(你可以使用辅助函数)

     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 年前的,但这是我现在需要的东西,这就是我做的事情感谢阅读

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM