gpt4 book ai didi

android - 在 Activity 开始时获得通知/接收明确的 Intent

转载 作者:太空宇宙 更新时间:2023-11-03 11:23:39 25 4
gpt4 key购买 nike

我正在开发一个应用程序,它会在用户选择的 Activity 启动时收到通知。为此,最好的方法是为 ACTION_MAIN 显式 Intent 注册一个 BroadcastReceiver,据我所知这不是工作(因为这些 Intent 有特定的目标)。另一种可能效率较低的方法是使用系统 ActivityManager 并轮询 getRunningTask(),它会返回当前所有正在运行的任务的列表。轮询可以由后台服务完成。通过监视此列表中的更改,我可以查看 Activity 是否正在运行,以便我的应用程序可以得到通知。缺点当然是投票。我还没有尝试过,但我认为最后一种方法可能会奏效。

有没有人知道更好的方法或强度较低的建议?

最佳答案

为什么不能直接调用 getParent()

这将在您的 child Activity

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = new Intent();

//Create this method in your Parent Activity
getParent().onChildCreated(this, intent);
}

这将在您的父 Activity

public void onChildCreated(Activity child, Intent intent) {
/*
* Have fun (Edited to pass intent)
*/
}

关于android - 在 Activity 开始时获得通知/接收明确的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2507552/

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