Android L 5.0版本获取topActivity的方法
2015-08-20 17:19
531 查看
Android L版本中getRunningTasks已经失效
需要添加权限:
[/code]
参考:
http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l http://blog.csdn.net/wulianghuan/article/details/46348043
需要添加权限:
<uses-permission android:name="android.permission.GET_TASKS"/>
[/code]
public static String getTopPkgName(Context context) { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { Field field = null; try { field = RunningAppProcessInfo.class .getDeclaredField("processState"); } catch (NoSuchFieldException e) { e.printStackTrace(); } List<ActivityManager.RunningAppProcessInfo> processInfos = am .getRunningAppProcesses(); for (RunningAppProcessInfo app : processInfos) { if (app.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && app.importanceReasonCode == 0) { Integer state = null; try { state = field.getInt(app); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } if (state != null && state == 2) { if (app.pkgList.length > 0) { Mlog.d(TAG, "---L getTopPkgName: " + app.pkgList[0]); return app.pkgList[0]; } } } } } else { List<RunningTaskInfo> runningTasks = am.getRunningTasks(1); if (runningTasks != null && runningTasks.size() > 0) { RunningTaskInfo runningTaskInfo = runningTasks.get(0); ComponentName topActivity = runningTaskInfo.topActivity; String packageName = topActivity.getPackageName(); Mlog.d(TAG, "---getTopPkgName: " + packageName); return packageName; } } Mlog.d(TAG, "---getTopPkgName: NULL"); return null; }
参考:
http://stackoverflow.com/questions/24625936/getrunningtasks-doesnt-work-in-android-l http://blog.csdn.net/wulianghuan/article/details/46348043
相关文章推荐
- 安卓突击:Android 动画有哪几种?
- Android性能优化之内存篇-google
- Android中AsyncTask的简单用法
- android --学习导航
- Android开发最佳实践
- android studio快捷键大全
- Codelab for Android Design Support Library used in I/O Rewind Bangkok session
- Error: Target id 'android-5' is not valid. Use 'android list targets' to get the target ids.
- Android性能优化(一)
- Android 仿QQ首页的消息和电话的切换,首页的头部(完全用布局控制)
- 快捷android source
- How to distribute your own Android library through jCenter and Maven Central from Android Studio
- Introduction to Glide, Image Loader Library for Android, recommended by Google
- android学习笔记2 - button的监听
- android 自定义TextView"会发脾气的TextView"
- android 自定义TextView"会发脾气的TextView"
- 《AndroidStudio每日一贴》3.快速切换代码风格、配色方案和键盘
- 基于Android中Looper , Handler , Message的线程池,轻松解决Sqlite数据库的线程安全问题
- Android 快速开发系列 打造万能的ListView GridView 适配器
- Android下JSON解析的方式