码迷,mamicode.com
首页 > 移动开发 > 详细

Android获得栈中最顶层的Activity

时间:2018-09-13 16:24:56      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:===   except   ignore   star   Owner   eth   style   int   otf   

 

 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

标签:===   except   ignore   star   Owner   eth   style   int   otf   

原文地址:https://www.cnblogs.com/hello-studio/p/9640504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!