赞
踩
分两种情况:
1.当手机连接usb电源时,息屏不会造成定时器暂停运行
2.当手机未连接usb电源时,息屏会造成定时器暂停运行,并在再次点亮屏幕时定时器重新开始工作
测试:
1.service保活,在service中启动定时器,无法解决
2.开启WakeLock无法解决
3.使用AlarmManager、CountDownTimer、Handler、Timer定时器,无法解决
……
分析:
通过网上的一系列方法都不能很好的解决这个问题,所以归根结底还是回到手机本身系统来看这个问题。随着android版本的一天天升级,普通软件的后台运行也越来越难搞(当然跟微信、qq这种厂商的合作软件无法相比)。现在手机主要有两大功能影响后台运行,第一个是用户必须允许软件能自启动后台运行,第二个用户要关闭此软件的省电权限(加入白名单)。
解决:
1.关闭省电权限(加入白名单)
先添加权限
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
判断并申请加入白名单
- /**
- * 判断我们的应用是否在白名单中
- *
- * @param context
- * @return
- */
- @RequiresApi(api = Build.VERSION_CODES.M)
- public static boolean isIgnoringBatteryOptimizations(Context context) {
- boolean isIgnoring = false;
- PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
- if (powerManager != null) {
- isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
- }
- return isIgnoring;
- }
-
- /**
- * 申请加入白名单
- *
- * @param context
- */
- @RequiresApi(api = Build.VERSION_CODES.M)
- public static void requestIgnoreBatteryOptimizations(Context context) {
- try {
- Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
- intent.setData(Uri.parse("package:" + context.getPackageName()));
- context.startActivity(intent);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
华为请求加入白名单后就会弹出提示框,点击允许。
2.开启软件可自启动能后台运行权限
-
- /**
- * 开启软件可自启动,后台运行 需进入页面自己手动设置
- */
- public static void openAutoStart(Context context) {
- try {
- if (isHuawei()) {
- goHuaweiSetting(context);
- } else if (isXiaomi()) {
- goXiaomiSetting(context);
- } else if (isOPPO()) {
- goOPPOSetting(context);
- } else if (isVIVO()) {
- goVIVOSetting(context);
- } else if (isMeizu()) {
- goMeizuSetting(context);
- } else if (isSamsung()) {
- goSamsungSetting(context);
- } else if (isLeTV()) {
- goLetvSetting(context);
- } else if (isSmartisan()) {
- goSmartisanSetting(context);
- }else {
- Intent intent = new Intent();
- 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);
- }
- }catch (Exception e){
- Intent intent = new Intent(Settings.ACTION_SETTINGS);
- context.startActivity(intent);
- }
- }
- /**
- * 跳转到指定应用的首页
- */
- private static void showActivity(Context context, @NonNull String packageName) {
- Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
- context.startActivity(intent);
- }
-
- /**
- * 跳转到指定应用的指定页面
- */
- private static void showActivity(Context context, @NonNull String packageName, @NonNull String activityDir) {
- Intent intent = new Intent();
- intent.setComponent(new ComponentName(packageName, activityDir));
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(intent);
- }
- /**
- * 华为厂商判断
- *
- * @return
- */
- public static boolean isHuawei() {
- if (Build.BRAND == null) {
- return false;
- } else {
- return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");
- }
- }
- /**
- * 小米厂商判断
- *
- * @return
- */
- public static boolean isXiaomi() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi");
- }
- /**
- * OPPO厂商判断
- *
- * @return
- */
- public static boolean isOPPO() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("oppo");
- }
- /**
- * VIVO厂商判断
- *
- * @return
- */
- public static boolean isVIVO() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("vivo");
- }
- /**
- * 魅族厂商判断
- *
- * @return
- */
- public static boolean isMeizu() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("meizu");
- }
- /**
- * 三星厂商判断
- *
- * @return
- */
- public static boolean isSamsung() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("samsung");
- }
- /**
- * 乐视厂商判断
- *
- * @return
- */
- public static boolean isLeTV() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("letv");
- }
- /**
- * 锤子厂商判断
- *
- * @return
- */
- public static boolean isSmartisan() {
- return Build.BRAND != null && Build.BRAND.toLowerCase().equals("smartisan");
- }
-
- /**
- * 跳转华为手机管家的启动管理页
- * 操作步骤:应用启动管理 -> 关闭应用开关 -> 打开允许自启动
- *
- * @param context
- */
- public static void goHuaweiSetting(Context context) {
- try {
- showActivity(context, "com.huawei.systemmanager",
- "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
- } catch (Exception e) {
- showActivity(context, "com.huawei.systemmanager",
- "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
- }
- }
-
- /**
- * 跳转小米安全中心的自启动管理页面
- * 操作步骤:授权管理 -> 自启动管理 -> 允许应用自启动
- *
- * @param context
- */
- public static void goXiaomiSetting(Context context) {
- showActivity(context, "com.miui.securitycenter",
- "com.miui.permcenter.autostart.AutoStartManagementActivity");
- }
-
- /**
- * 跳转 OPPO 手机管家
- * 操作步骤:权限隐私 -> 自启动管理 -> 允许应用自启动
- *
- * @param context
- */
- public static void goOPPOSetting(Context context) {
- try {
- showActivity(context, "com.coloros.phonemanager");
- } catch (Exception e1) {
- try {
- showActivity(context, "com.oppo.safe");
- } catch (Exception e2) {
- try {
- showActivity(context, "com.coloros.oppoguardelf");
- } catch (Exception e3) {
- showActivity(context, "com.coloros.safecenter");
- }
- }
- }
- }
-
- /**
- * 跳转 VIVO 手机管家
- * 操作步骤:权限管理 -> 自启动 -> 允许应用自启动
- *
- * @param context
- */
- public static void goVIVOSetting(Context context) {
- showActivity(context, "com.iqoo.secure");
- }
-
- /**
- * 跳转魅族手机管家
- * 操作步骤:权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行
- *
- * @param context
- */
- public static void goMeizuSetting(Context context) {
- showActivity(context, "com.meizu.safe");
- }
-
- /**
- * 跳转三星智能管理器
- * 操作步骤:自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用
- *
- * @param context
- */
- public static void goSamsungSetting(Context context) {
- try {
- showActivity(context, "com.samsung.android.sm_cn");
- } catch (Exception e) {
- showActivity(context, "com.samsung.android.sm");
- }
- }
-
- /**
- * 跳转乐视手机管家
- * 操作步骤:自启动管理 -> 允许应用自启动
- *
- * @param context
- */
- public static void goLetvSetting(Context context) {
- showActivity(context, "com.letv.android.letvsafe",
- "com.letv.android.letvsafe.AutobootManageActivity");
- }
- /**
- * 跳转锤子手机管家
- * 操作步骤:自启动管理 -> 允许应用自启动
- *
- * @param context
- */
- public static void goSmartisanSetting(Context context) {
- showActivity(context, "com.smartisanos.security");
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
华为跳转页面如下,然后手动打开自启动后台运行
这样操作下来,不是特殊情况下息屏后定时器就能持续运行了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。