我想从下面的代码中获取上下文。
我无法找到如何获得活动所以请帮助我。
代码:
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();
先谢谢
答案 0 :(得分:0)
如何从ActivityManager.RunningTaskInfo获取上下文?
使用热门活动包名称调用createPackageContext
方法获取上下文:
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
String packageName = ar.topActivity.getPackageName();
Context otherAppContext=getApplicatoinContext().createPackageContext(packageName,
Context.CONTEXT_IGNORE_SECURITY);