由于Linux的开源,市面上出现了很多定制的Android系统,这写系统有的将Android原生系统改的面目全非,很多方法不能同时在所有机型上适用,这是问题的一大难点。


项目中遇见了一个问题,我用JPush SDK推送消息,但是如果服务被关闭,将无法及时接收消息。这样也就没有起到应有的效果。所以我想让我的app接收系统广播,每次接收到Intent.ACTION_TIME_TICK这个广播我就去检查服务是否被关闭。可是这样就要面对另外一个问题:当用户使用第三方清理软件清理系统后,我的app就被标记为了stopped状态,无法再接收到系统广播,解决方法我倒是有一个,那就是让用户把我的app添加进自启动白名单里面,这样手机就能够接收系统广播了。当然你也可以把app安装进系统目录,这样就能像系统app一样接收系统广播了,但是我们不是流氓软件,所以我们没打算这样做。


这就需要我们拉起系统自启动设置页面,下面给出代码:

public class OpenAutoStartUtil {    /**     * Get Mobile Type     *     * @return     */    private static String getMobileType() {        return Build.MANUFACTURER;    }    /**     * GoTo Open Self Setting Layout     * Compatible Mainstream Models 兼容市面主流机型     *     * @param context     */    public static void jumpStartInterface(Context context) {        Intent intent = new Intent();        try {            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);            Log.e("HLQ_Struggle", "******************当前手机型号为:" + getMobileType());            ComponentName componentName = null;            if (getMobileType().equals("Xiaomi")) { // 红米Note4测试通过                componentName = new ComponentName("com.miui.securitycenter",                        "com.miui.permcenter.autostart.AutoStartManagementActivity");            } else if (getMobileType().equals("Letv")) { // 乐视2测试通过                intent.setAction("com.letv.android.permissionautoboot");            } else if (getMobileType().equals("samsung")) { // 三星Note5测试通过                componentName = new ComponentName("com.samsung.android.sm_cn",                        "com.samsung.android.sm.ui.ram.AutoRunActivity");            } else if (getMobileType().equals("HUAWEI")) { // 华为测试通过                componentName = new ComponentName("com.huawei.systemmanager",                        "com.huawei.systemmanager.optimize.process.ProtectActivity");            } else if (getMobileType().equals("vivo")) { // VIVO测试通过                componentName = ComponentName.unflattenFromString("com.iqoo.secure" +                        "/.safeguard.PurviewTabActivity");            } else if (getMobileType().equals("Meizu")) { //万恶的魅族                // 通过测试,发现魅族是真恶心,也是够了,之前版本还能查看到关于设置自启动这一界面,                // 系统更新之后,完全找不到了,心里默默Fuck                // 针对魅族,我们只能通过魅族内置手机管家去设置自启动,                // 所以我在这里直接跳转到魅族内置手机管家界面,具体结果请看图                componentName = ComponentName.unflattenFromString("com.meizu.safe" +                        "/.permission.PermissionMainActivity");            } else if (getMobileType().equals("OPPO")) { // OPPO R8205测试通过                componentName = ComponentName.unflattenFromString("com.oppo.safe" +                        "/.permission.startup.StartupAppListActivity");                Intent intentOppo = new Intent();                intentOppo.setClassName("com.oppo.safe/.permission.startup",                        "StartupAppListActivity");                if (context.getPackageManager().resolveActivity(intentOppo, 0) == null) {                    componentName = ComponentName.unflattenFromString("com.coloros.safecenter" +                            "/.startupapp.StartupAppListActivity");                }            } else if (getMobileType().equals("ulong")) { // 360手机 未测试                componentName = new ComponentName("com.yulong.android.coolsafe",                        ".ui.activity.autorun.AutoRunListActivity");            } else {                             // 以上只是市面上主流机型,由于公司你懂的,所以很不容易才凑齐以上设备                // 针对于其他设备,我们只能调整当前系统app查看详情界面                // 在此根据用户手机当前版本跳转系统设置界面                if (Build.VERSION.SDK_INT >= 9) {                    intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");                    intent.setData(Uri.fromParts("package", context.getPackageName(), null));                } else if (Build.VERSION.SDK_INT <= 8) {                    intent.setAction(Intent.ACTION_VIEW);                    intent.setClassName("com.android.settings",                            "com.android.settings.InstalledAppDetails");                    intent.putExtra("com.android.settings.ApplicationPkgName",                            context.getPackageName());                }            }            intent.setComponent(componentName);            context.startActivity(intent);        } catch (Exception e) {//抛出异常就直接打开设置页面            intent = new Intent(Settings.ACTION_SETTINGS);            context.startActivity(intent);        }    }}

这样的方法我们可以适配市面上大部分手机了。

本来我是想过用代码将自己的app设置为开机自启动,但是从网络上找了很多方法,最后还是没有成功,希望有这方面经验的大神不吝赐教,拜谢!


想到我们打开软件的时候就应该判断我们的app是否已经被允许自启动了,我又遇见了一个难题,至今未能解决。怎么样才能像手机安全卫士一样获取到有哪些应用被允许自启动了呢?网上有人说看是否有开机启动权限,不可行,因为有权限也不能保证开机启动,因为用户如果禁止了,你的权限就不起作用了。还有看到一个大神的方法,记得好像是获取自启动的状态,但是不管用户允许了自启动还是禁止了,其状态都是0(默认),这方法也不可行。有哪位大神知道的话,还请告知,拜谢!




更多相关文章

  1. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  2. Android(安卓)nomedia 避免图片等资源泄露在系统图库当中
  3. Eclipse如何快速调试系统App(具有系统权限的Android(安卓)App)即Ec
  4. 基于Android的温湿度采集系统
  5. WPS Office移动版(Android)限时免费,送小米手机
  6. Android(安卓)将自己的Launcher设置为默认启动
  7. Android(安卓)之 悬浮窗
  8. Android优化——绘制优化之android系统显示原理(一)
  9. 利用ADB固定Android手机的CPU频率

随机推荐

  1. 自定义Dialog对话框
  2. Android Gradle Release Version 2.4 增
  3. Android之SurfaceView学习(一)
  4. android 使用zxing生成中间嵌套logo的二
  5. android捕获全局异常,CrashHandler
  6. 导入android自带通讯录报错
  7. 修复Android Studio 3.5 AIDL报错问题
  8. ntfs-3g 移植到android
  9. Android培训班(24)
  10. 源码分析android的UI绘制流程