Android获得栈中最顶层的Activity

 

 1 /**
 2      * 获得栈中最顶层的Activity
 3      *
 4      * @param context
 5      * @return
 6      */
 7     public String getTopActivity(Context context)
 8     {
 9         android.app.ActivityManager manager = (android.app.ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
10         List<ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
11 
12         if (runningTaskInfos != null)
13         {
14             return (runningTaskInfos.get(0).topActivity.getClassName());
15         } else
16             return null;
17     }
18 
19 try
20         {
21             Intent intent = new Intent(MainActivity.this, Class.forName("com.johnson.main.Activity1"));
22             startActivity(intent);
23         } catch (ClassNotFoundException e)
24         {
25             e.printStackTrace();
26         }
27 
28 
29 ================
30 
31 
32       Context context = createPackageContext("com.johnson.main.common.Activity2",
                             Context.CONTEXT_INCLUDE_CODE |
                            Context.CONTEXT_IGNORE_SECURITY); 33 openDiaog((Activity) context); 35 Class clazz = context.getClassLoader().loadClass(topActivity);//载入这个类 36 Object owner = clazz.newInstance(); //新建一个实例 37 Object obj = clazz.getMethod("openDiaog", String.class).invoke(owner, context); //获取print方法,传入参数并执行

 

Android获得栈中最顶层的Activity

上一篇:Android使用自定义字体


下一篇:jenkins+gradle+上传蒲公英(Android持续集成)