0

使用包信息启动/显示如何检测家庭活动。就像我们通过这段代码获取包名一样:

ActivityManager.RunningTaskInfo localRunningTaskInfo = (ActivityManager.RunningTaskInfo)((ActivityManager)context.getSystemService("activity")).getRunningTasks(1).get(0);
String str1 = localRunningTaskInfo.topActivity.getPackageName();
String str2 = localRunningTaskInfo.topActivity.getClassName();

有了这个,我们得到所有应用程序包名和类名。如何检测首页是否显示?

4

1 回答 1

0

您可以使用ActivityManager来获取它。以下是示例代码:

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);


 // get the info from the currently running task
 List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

 Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());

 ComponentName componentInfo = taskInfo.get(0).topActivity;
 String packageName = componentInfo.getPackageName();

您的清单需要以下权限:

uses-permission android:name="android.permission.GET_TASKS"

示例项目

从 logcat 你可以看到哪个活动被启动了..

于 2013-10-24T11:01:23.450 回答