如何从 ActivityManager.RunningTaskInfo 获取上下文?

How to get context from ActivityManager.RunningTaskInfo?

我想从下面的代码中获取上下文。

我找不到如何获得 activity 所以请帮助我。

代码:

ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
String activityOnTop=ar.topActivity.getClassName();

提前致谢

How to get context from ActivityManager.RunningTaskInfo?

调用createPackageContext方法使用topActivity包名获取上下文:

ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
String packageName = ar.topActivity.getPackageName();
Context otherAppContext=getApplicatoinContext().createPackageContext(packageName,
                                                   Context.CONTEXT_IGNORE_SECURITY);