gpt4 book ai didi

java - 从广播接收器 android 调用 Activity 警报框

转载 作者:行者123 更新时间:2023-12-01 14:20:23 26 4
gpt4 key购买 nike

我有一个可以启动广播的应用程序。如果应用程序正在运行,我想显示一个弹出窗口,无论我在哪个 Activity 上,如果没有运行,则推送通知

这是我的代码

public class AlarmReceiver extends BroadcastReceiver
{
private static int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;

@Override
public void onReceive(final Context ctx, Intent intent)
{
if (isRunning(ctx))
{
Intent myIntent = new Intent (ctx, NotifyTagabilityCounter.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(myIntent);
}
else
{
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);

Intent i = new Intent(ctx, Game.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(Constants.TAG_TIMESTAMP, true);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
i, PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.launcher)

Notification notification = mBuilder.getNotification();
notification.flags = Notification.FLAG_AUTO_CANCEL;

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);

SharedPreferences customSharedPreference = ctx.getSharedPreferences(
"UserSharedPrefs", Activity.MODE_PRIVATE);

SharedPreferences.Editor editor = customSharedPreference.edit();
editor.putBoolean(Constants.TAG_NOTIFICATION, true);
editor.commit();
}
}

public boolean isRunning(Context ctx)
{
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

for (RunningTaskInfo task : tasks)
{
if (ctx.getPackageName().equalsIgnoreCase(
task.baseActivity.getPackageName()))
return true;
}

return false;
}

创建新 Activity NotificyTagabilityCounter

ublic class NotifyTagabilityCounter extends Activity
{
public static final int NOTIFICATION_ID_RECEIVED = 0x1221;
static final String ACTION = "com.trib.broadcast";

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ACTION);
this.registerReceiver(mReceivedReceiver, filter);
}

private final BroadcastReceiver mReceivedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if (ACTION.equals(action))
{
displayAlert();
}
}
};

我得到的只是黑屏。我希望显示我的最后一个 Activity ,并在其顶部显示一个弹出式 AlertDisplay。这怎么能做到..?

最佳答案

要做到这一点:
1-定义两个BroadcastReceivers list 中的第一个优先级较低,应用程序(运行时)中每个 Activity 中的第二个优先级较高。
第一名BroadcastReceivers应显示通知
第二次广播应该 abort()广播并显示对话框。

2-发送有序广播。

PS:您可以创建一个扩展 Activity 的 BaseActivtiy,并在其上定义第二个广播(不要忘记在 onStart()onStop() 中注册和取消注册广播),
然后让你的所有 Activity 都扩展 BaseActivtiy

关于java - 从广播接收器 android 调用 Activity 警报框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17642329/

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