Java Android - 在前台动态获取当前活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18115493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:29:28  来源:igfitidea点击:

Android - Dynamically Get the Current Activity in Foreground

javaandroiddynamicandroid-activityget

提问by Dylan Holmes

here is the situation:

这是情况:

  1. In a Thread an event is triggered.
  2. The current activity needs to be retrieved.
  3. Then a Dialog Box is created on that activity and shown.
  1. 在线程中触发事件。
  2. 需要检索当前活动。
  3. 然后在该活动上创建一个对话框并显示。

Problems: As far as I've searched there is no way to retrieve the current activity in the foreground.

问题:据我搜索,没有办法在前台检索当前活动。

Extra info: This needs to be able to be handled in multiple activities. So, it can be popped-up in Activity-A or B or C.

额外信息:这需要能够在多个活动中处理。因此,它可以在 Activity-A 或 B 或 C 中弹出。

采纳答案by Prmths

I'm not sure if this is what you're looking for, but it seemed pretty straightforward. http://iamvijayakumar.blogspot.com/2011/09/get-current-activity-and-package-name.html

我不确定这是否是您要找的,但这似乎很简单。http://iamvijayakumar.blogspot.com/2011/09/get-current-activity-and-package-name.html

  ActivityManager am = (ActivityManager) this .getSystemService(ACTIVITY_SERVICE);
  List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
  ComponentName componentInfo = taskInfo.get(0).topActivity;
  Log.d(WebServiceHelper.TAG, "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName()+"   Package Name :  "+componentInfo.getPackageName());

Hope this helps.

希望这可以帮助。

回答by CommonsWare

Plan A: taking your request literally

计划 A:从字面上理解您的要求

Step #1: Have the thread be managed by a service

第 1 步:让线程由服务管理

Step #2: have the service send a message when the "even is triggered" -- LocalBroadcastManager, Square's Otto, greenrobot's EventBus, etc.

第 2 步:让服务在“偶数被触发”时发送消息LocalBroadcastManager——Square 的 Otto、greenrobot 的 EventBus 等。

Step #3: Have each activity be set up to listen for that message when it is in the foreground

第 3 步:将每个 Activity 设置为在该消息处于前台时侦听该消息

Step #4: Have the activity display the dialog upon receipt of the message

第 4 步:让活动在收到消息后显示对话框



Plan B: same visual result

方案 B:相同的视觉效果

Step #1: Have the thread be managed by a service

第 1 步:让线程由服务管理

Step #2: Have the service call startActivity()on a dialog-themed activity

第 2 步:startActivity()在以对话为主题的活动上调用服务

Step #3: There is no step #3

第 3 步:没有第 3 步

回答by Sushil

You can achieve this using a custom broadcast receiver. You can define a broadcast receiver in your thread and write the receiver in different activities. And from your thread you can decide which activity to send broadcast.

您可以使用自定义广播接收器来实现这一点。您可以在线程中定义广播接收器并在不同的活动中编写接收器。从您的线程中,您可以决定要发送广播的活动。

In the receiver, you can show the dialog.

在接收器中,您可以显示对话框。

回答by frogmanx

You may do that by extending Activity and Application and having methods that get and set the current viewed Activity reference as an Application level variable. Try this approach: How to get current foreground activity context in android?.

您可以通过扩展 Activity 和 Application 并使用获取和设置当前查看的 Activity 引用作为应用程序级别变量的方法来做到这一点。试试这个方法:How to get current Foreground Activity context in android? .

回答by Xian Zhang

Use Face book's stethopackage is safest and simplest way for me (and no complaints from other team members that "it's hacky we won't approve!" thing...

使用Facebook 的 stetho包对我来说是最安全和最简单的方法(并且其他团队成员没有抱怨“我们不会批准它的hacky!”事情......

Just as simple as:

就这么简单:

ActivityTracker.get().tryGetTopActivity();

ActivityTracker.get().tryGetTopActivity();

回答by justnoone

just like @Xian Zhang said, using Facebook stetholib is working for me

就像@Xian Zhang 说的,使用Facebook stetholib 对我有用

ActivityTracker.get().tryGetTopActivity()?.localClassName

If your activity is within the package ui , the output be like

如果您的活动在包 ui 内,则输出类似于

    ui.YourActivityName

your app package name will not be included

您的应用程序包名称将不包含在内