gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:37:24 25 4
gpt4 key购买 nike

我想在点击通知中的按钮时发送 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是支持包的一部分(您添加到应用程序中的可选库),而不是系统本身的一部分。

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

关于android - 是否可以从通知操作发送 LocalBroadcast?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22417918/

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