自启动管理简介
Android手机上安装的很多应用都会自启动,占用资源越来越多,造成系统卡顿等现象。良好的自启动管理方案管理后台自启动和开机自启动,这样就可以节约内存、优化系统流畅性等。
自启动管理流程分析
初始化
AutoRun.apk的自定义类BootBroadcastReceiver接收到AutoRunCore.apk发过来的广播后,开启线程设置禁止自启动列表和允许自启动列表;从R.array.security_boot_run_applist数组中获取允许自启动列表,定义为白名单,白名单中保存的均是非系统应用;从R.array.security_boot_forbidrun_applist数组中获取禁止自启动的列表,定义为黑名单,黑名单中保存的均是系统应用。最终调用系统接口将禁止自启动的应用(包括黑名单中的系统应用、不在白名单中的非系统应用)全部写到/data/system/forbidden_autorun_packages.xml文件中。
关键代码如下:
1、注册广播接收器
<receiver
android:name="com.android.BootBroadcastReceiver"
android:exported="true"
android:priority="1000"
android:process="@string/process_security" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.security.BOOT_COMPLETED" />
</intent-filter>
</receiver>
2、接收到广播后处理方法
@Override public void onReceive(final Context context, Intent intent) { action = intent.getAction(); if((action != null)&&(action.equals("android.intent.action.security.BOOT_COMPLETED"))){ action = ACTION_BOOT_COMPLETED; } ...... if(BOOT_AUTO_RUN && ACTION_BOOT_COMPLETED.equals(action)) { Log.d("forbid auto run"); new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub SharedPreferences preferences = context.getSharedPreferences( "forbidrun_appList", Context.MODE_PRIVATE); boolean isInit = preferences.getBoolean("initflag", false); long lastModif = preferences.getLong("fileModif", 0); long fileModif = 0; File config = new File("data/system/seccenter/" + APPAUTORUN_CONFIG_FILE);//APPAUTORUN_CONFIG_FILE = "seccenter_appautorun_applist.xml" pm = context.getPackageManager(); List<String> autorun_appList = new ArrayList<String>(); if (config.exists()) { fileModif = config.lastModified(); } if (!isInit || (fileModif > lastModif)) { if (config.exists()) { try { autorun_appList = parseXML(config); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { autorun_appList = Arrays.asList(context.getResources() .getStringArray(R.array.security_boot_run_applist)); } List<String> forbidrun_appList = Arrays.asList(context.getResources() .getStringArray(R.array.security_boot_forbidrun_applist)); List<ApplicationInfo> allAppInfo = pm.getInstalledApplications(0); for (ApplicationInfo appInfo : allAppInfo) { if (!Util.isSystemApp(appInfo) && !autorun_appList.contains(appInfo.packageName)) { SystemApiUtil.fobidAutoRun(context,appInfo.packageName, true); } else if (Util.isSystemApp(appInfo) && forbidrun_appList.contains(appInfo.packageName)) { SystemApiUtil.fobidAutoRun(context,appInfo.packageName, true); } } SharedPreferences preference = context.getSharedPreferences("forbidrun_appList", Context.MODE_PRIVATE); SharedPreferences.Editor editor = preference.edit(); editor.putBoolean("initflag", true); editor.putLong("fileModif", fileModif); editor.commit(); } } }, "btReForbidRun").start(); }
在上面的处理方法中使用的几个封装的方法,下面逐一看下。先看parseXML()方法,
public List<String> parseXML(File xmlFile) throws Exception { List<String> appList = new ArrayList<String>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(xmlFile); NodeList nodeList = doc.getElementsByTagName("appname"); Node fatherNode = nodeList.item(0); NodeList childNodes = fatherNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (childNode instanceof Element) { appList.add(childNode.getFirstChild().getNodeValue()); } } return appList; }
接着看下Util.isSystemApp()方法,
public static boolean isSystemApp(ApplicationInfo appInfo) { boolean flag = false; if (appInfo != null && ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 || (appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)) { flag = true; } return flag; }
再接着看下forbitAutorun()方法,这里是通过反射方式调用ActivityManager中的方法。
private static Method mSetStopAutoStart = null;private static Method mgetStopAutoStart = null;
public static void fobidAutoRun(Context context, String pkg, boolean isFobid) { if (isForceStopAutoStartMethodExist(context)) { try { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); mSetStopAutoStart.invoke(am, pkg, isFobid); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } public static boolean isForceStopAutoStartMethodExist(Context context) { synchronized (SystemApiUtil.class) { if (mSetStopAutoStart == null) { try { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); mSetStopAutoStart = am.getClass().getMethod( "setForbiddenAutorunPackages", String.class, boolean.class); mgetSto
今天的文章安卓自启动管理软件哪个好_什么是自启动应用分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/58713.html