调用服务的活动或接收者的名称

发布于 2024-12-02 22:10:45 字数 94 浏览 1 评论 0原文

我有一个从接收者或活动调用的意图服务。我想知道触发服务的接收者或活动的名称。我不想使用任何额外的内容或标志来传递给服务。

有没有办法在代码运行时查看活动堆栈?

I have a Intent service which is called from either a receiver or an activity. I would like to know the name of the receiver or activity that triggers the service. I don't want to use any extras or flags to pass to the service.

Is there a way to see the activity stack while the code is on the run?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-09 22:10:45

据我所知,没有办法检测意图的发送者。

As far as I know there is no way to detect sender of the Intent.

零度℉ 2024-12-09 22:10:45

有没有办法在代码运行时查看活动堆栈?

您可以使用 ActivityManager.RunningTaskInfo。虽然它没有提供太多 API,但可能足以满足您的要求。伪代码:

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(10);
ActivityManager.RunningTaskInfo firstTask = runningTasks.get(0);
String topActivityName = firstTask.topActivity.getShortClassName();
String rootActivityName = firstTask.baseActivity.getShortClassName();

它使您能够检索特定任务(又称后堆栈)的顶部和根活动,请注意,您需要设置 persimmon GET_TASKS

Is there a way to see the activity stack while the code is on the run?

You can use ActivityManager.RunningTaskInfo. Though it doesn't provide much API, it is probably sufficient for your requirements. Pseudocode:

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(10);
ActivityManager.RunningTaskInfo firstTask = runningTasks.get(0);
String topActivityName = firstTask.topActivity.getShortClassName();
String rootActivityName = firstTask.baseActivity.getShortClassName();

It gives you ability to retrieve the top and root activities of a specific task (AKA. back stack), Note that you need set persimmon GET_TASKS in AndroidManifest.xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文