美文网首页
Android 启动前台服务,适配 vivo 与 OPPO 手机

Android 启动前台服务,适配 vivo 与 OPPO 手机

作者: badmask | 来源:发表于2017-09-18 20:13 被阅读0次

    Android 启动前台服务,华为、小米、三星、OPPO、VIVO的配合程度接 这是前文链接,我先甩在这里,上篇文章主要分析了几种品牌手机关于通知的配合程度,这篇文章先写第一期的解决方案,毕竟需求还是要跟版发布的~

    因为测试的「华为 6.0」「华为 4.4.2」「小米6」「红米note 4X 6.0」「小米5.1.1」「三星手机 5.0.1」这几种机型可以正常开启前台服务,「vivo X9Plus 」「vivo X9 」「OPPO A59s」「OPPO R9s」不能开启前台服务,所以第一期先对 vivo 与 OPPO 做一个特殊的处理。

    在 App 启动之后的适当时机,用下面这段代码
    逻辑:判断是否是 vivo or OPPO 手机,如果是,再判断通知是否可用,如果不可用再弹出提示框引导用户进入设置界面。

            if (Build.MANUFACTURER.equals("vivo") || Build.MANUFACTURER.equals("OPPO")){
                if (!NotificationUtils.isNotificationEnabled(this)){
                    //未获得通知权限,此时把用户引向系统的设置界面,使用户手动打开通知权限
                    //代码不上了,总之在这里谈提示框让用户打开设置界面就好了~
                }
            }
    

    检测通知是否可用的方法:

        @SuppressLint("NewApi")
        public static boolean isNotificationEnabled(Context context) {
            AppOpsManager appOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
            ApplicationInfo applicationInfo = context.getApplicationInfo();
            String pkg = context.getApplicationContext().getPackageName();
            int uid = applicationInfo.uid;
            Class appOpsClass;
            try {
                appOpsClass = Class.forName(AppOpsManager.class.getName());
                Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
                Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
                int value = (Integer) opPostNotificationValue.get(Integer.class);
                return ((Integer) checkOpNoThrowMethod.invoke(appOpsManager, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return false;
        }
    

    开启通知服务的代码:

        /**
         * 未发现不能执行「开启通知」操作的机型
         * 能够进入「设置」页面的机型有:vivo X9 6.0系统 / vivo X9Plus 6.0系统 / OPPO R9s 6.0系统 / 华为ATH-TL00 6.0系统
         */
        public static void toSettingPage(Context context){
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            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());
            }
            context.startActivity(intent);
        }
    

    第一期先这样处理,然后继续调研~

    相关文章

      网友评论

          本文标题:Android 启动前台服务,适配 vivo 与 OPPO 手机

          本文链接:https://www.haomeiwen.com/subject/wcivsxtx.html