About
基本功能: 获取设备上所有应用的开机启动完成的广播接收者,同时区分系统应用和用户应用.
然后,提供界面接口给用户,统一管理应用的自启动功能,这样可以提升系统的运行性能和待机时长;
获取所有receivers 区分是否使能
获取包含被禁止自启动receiver的信息
1
| List<ResolveInfo> forbidInfoList = mPackageManager.queryBroadcastReceivers(intent,PackageManager.GET_DISABLED_COMPONENTS);
|
获取所有使能的 receivers
1
| List<ResolveInfo> allowInfoList = mPackageManager.queryBroadcastReceivers(intent,PackageManager.GET_RECEIVERS);
|
两个列表比较,标志receivers是否开启
1 2 3 4 5
| for(int k=0; k<allowInfoList.size(); k++){ if( allowInfoList.get(k).activityInfo.packageName.equals(packageName) ){ isEnable =true; } }
|
判断是否为系统应用
通过ApplicationInfo.FLAG_SYSTEM标志,判断是否为系统应用
1 2 3 4
| if((forbidInfoList.get(i).activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)==1|| (forbidInfoList.get(i).activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)==1){ isSystem = true; }
|
判断是否运行并获取内存占用大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| float memUsed = 0; ArrayList<Integer> pid = new ArrayList<Integer>(runningApps.size()); for(int k=0; k<runningApps.size(); k++){ if(runningApps.get(k).processName.equals(packageName)){ pid.add( Integer.valueOf(runningApps.get(k).pid) ); }else{ continue; } } if( pid.size() == 0){ isRun = false; }else{ isRun = true; int[] pids = new int[pid.size()]; for (int num=0; num<pid.size(); num++) { pids[num] = pid.get(num).intValue(); } Debug.MemoryInfo[] mi = mActivityManager.getProcessMemoryInfo(pids); for (int num=0; num<mi.length; num++) { memUsed += (float)(mi[num].getTotalPrivateDirty()+mi[num].getTotalPss())/1024; } }
|
关闭和开启Receiver
通过PackageManager.setComponentEnabledSetting方法,关闭和开启Receiver
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| private void updatePackageReceiverState( PackageEntry item, boolean isChecked){ String packageReceiverList[] = item.getPackageReceivers().split(";"); int newState = isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; for (int i=0; i<packageReceiverList.length; i++) { if( packageReceiverList[i] != null ){ ComponentName comName = new ComponentName(item.getPackageName(), packageReceiverList[i]); int flag = 0; if( item.getPackageName().equals("com.android.settings") ){ flag = PackageManager.DONT_KILL_APP; } mPackageManager.setComponentEnabledSetting(comName,newState, flag); } } }
|
其他功能
其他功能包括,列表排序,列表更新,无应用列表图片显示等,不在这里详细展开!