質問

I would like to send a LocalBroadcast when clicking on a button inside a notification. I know how to do that with a regular broadcast, but I would like to keep the broadcast inside my app. Is this possible?

The code I have is roughly:

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);

And also:

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
役に立ちましたか?

解決

No.

First of all, LocalBroadcastManager is part of the support package (an optional library you add to your application), not part of the system itself.

Secondly, even if it were, the notification service is used by all applications. Since it's a shared resource, there is nothing "local" that occurs when you post a notification.

他のヒント

this might work(it works for me)

add your BroadcastReceiver to 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>

creating a Notification and add action( .addaction() ) and sending Broadcast to BroadcastReceiver

(you can use the helper function)

     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;
 }

receive the action and create LocalBroadcast using LocalBroadcastManager based on your notification action

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);
    
  }

now you can receive the LocalBroadcast in any activity that is connect to BroadcastReceiver like this:

   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)));
}

this question was for 8 years ago but this was something i needed now and this is how i did it THANKS FOR READING

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top