网友通过本文主要向大家介绍了android5.0,android5.0新特性,android5.0系统下载,android5.0模拟器,android5.0系统手机等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名,android包名
- /**
- * 获取程序包名(本程序包名5.0版本上下都可获取)
- *
- * @return
- */
- public String getTaskPackname() {
- ActivityManager.RunningAppProcessInfo currentInfo = null;
- Field field = null;
- int START_TASK_TO_FRONT = 2;
- String currentApp = "CurrentNULL";
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
- try {
- field = ActivityManager.RunningAppProcessInfo.class.getDeclaredField("processState");
- } catch (Exception e) {
- e.printStackTrace();
- }
- ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningAppProcessInfo> appList = am.getRunningAppProcesses();
- for (ActivityManager.RunningAppProcessInfo app : appList) {
- if (app.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- Integer state = null;
- try {
- state = field.getInt(app);
- } catch (Exception e) {
- e.printStackTrace();
- }
- if (state != null && state == START_TASK_TO_FRONT) {
- currentInfo = app;
- break;
- }
- }
- }
- if (currentInfo != null) {
- currentApp = currentInfo.processName;
- }
- } else {
- ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
- List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
- currentApp = tasks.get(0).processName;
- }
- Log.e("TAG", "Current App in foreground is: " + currentApp);
- return currentApp;
- }
您可能想查找下面的文章:
- Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名,android包名
- Android5.0开发范例大全 读书笔记(五),android5.0范例
- Android5.0开发范例大全 读书笔记(六),android5.0范例
- Android5.0开发范例大全 读书笔记(三),android5.0范例
- Android5.0开发范例大全 读书笔记(四),android5.0范例
- android 5.0后对于apk 跑32 64 的逻辑
- Android 5.0 Settings源码简要分析
- Android5.0新特性-Material Design
- Android5.0之Toobar的使用
- Android 6.0 系统棉花糖新的特性和功能