当前位置:   article > 正文

android息屏休眠定时器心跳停止运行_安卓开发 锁屏后 timer

安卓开发 锁屏后 timer

分两种情况:

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" />

判断并申请加入白名单 

  1. /**
  2. * 判断我们的应用是否在白名单中
  3. *
  4. * @param context
  5. * @return
  6. */
  7. @RequiresApi(api = Build.VERSION_CODES.M)
  8. public static boolean isIgnoringBatteryOptimizations(Context context) {
  9. boolean isIgnoring = false;
  10. PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  11. if (powerManager != null) {
  12. isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
  13. }
  14. return isIgnoring;
  15. }
  16. /**
  17. * 申请加入白名单
  18. *
  19. * @param context
  20. */
  21. @RequiresApi(api = Build.VERSION_CODES.M)
  22. public static void requestIgnoreBatteryOptimizations(Context context) {
  23. try {
  24. Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
  25. intent.setData(Uri.parse("package:" + context.getPackageName()));
  26. context.startActivity(intent);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }

华为请求加入白名单后就会弹出提示框,点击允许。

2.开启软件可自启动能后台运行权限

  1. /**
  2. * 开启软件可自启动,后台运行 需进入页面自己手动设置
  3. */
  4. public static void openAutoStart(Context context) {
  5. try {
  6. if (isHuawei()) {
  7. goHuaweiSetting(context);
  8. } else if (isXiaomi()) {
  9. goXiaomiSetting(context);
  10. } else if (isOPPO()) {
  11. goOPPOSetting(context);
  12. } else if (isVIVO()) {
  13. goVIVOSetting(context);
  14. } else if (isMeizu()) {
  15. goMeizuSetting(context);
  16. } else if (isSamsung()) {
  17. goSamsungSetting(context);
  18. } else if (isLeTV()) {
  19. goLetvSetting(context);
  20. } else if (isSmartisan()) {
  21. goSmartisanSetting(context);
  22. }else {
  23. Intent intent = new Intent();
  24. if (Build.VERSION.SDK_INT >= 9) {
  25. intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
  26. intent.setData(Uri.fromParts("package", context.getPackageName(), null));
  27. } else if (Build.VERSION.SDK_INT <= 8) {
  28. intent.setAction(Intent.ACTION_VIEW);
  29. intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
  30. intent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
  31. }
  32. context.startActivity(intent);
  33. }
  34. }catch (Exception e){
  35. Intent intent = new Intent(Settings.ACTION_SETTINGS);
  36. context.startActivity(intent);
  37. }
  38. }
  39. /**
  40. * 跳转到指定应用的首页
  41. */
  42. private static void showActivity(Context context, @NonNull String packageName) {
  43. Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
  44. context.startActivity(intent);
  45. }
  46. /**
  47. * 跳转到指定应用的指定页面
  48. */
  49. private static void showActivity(Context context, @NonNull String packageName, @NonNull String activityDir) {
  50. Intent intent = new Intent();
  51. intent.setComponent(new ComponentName(packageName, activityDir));
  52. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  53. context.startActivity(intent);
  54. }
  55. /**
  56. * 华为厂商判断
  57. *
  58. * @return
  59. */
  60. public static boolean isHuawei() {
  61. if (Build.BRAND == null) {
  62. return false;
  63. } else {
  64. return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");
  65. }
  66. }
  67. /**
  68. * 小米厂商判断
  69. *
  70. * @return
  71. */
  72. public static boolean isXiaomi() {
  73. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi");
  74. }
  75. /**
  76. * OPPO厂商判断
  77. *
  78. * @return
  79. */
  80. public static boolean isOPPO() {
  81. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("oppo");
  82. }
  83. /**
  84. * VIVO厂商判断
  85. *
  86. * @return
  87. */
  88. public static boolean isVIVO() {
  89. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("vivo");
  90. }
  91. /**
  92. * 魅族厂商判断
  93. *
  94. * @return
  95. */
  96. public static boolean isMeizu() {
  97. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("meizu");
  98. }
  99. /**
  100. * 三星厂商判断
  101. *
  102. * @return
  103. */
  104. public static boolean isSamsung() {
  105. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("samsung");
  106. }
  107. /**
  108. * 乐视厂商判断
  109. *
  110. * @return
  111. */
  112. public static boolean isLeTV() {
  113. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("letv");
  114. }
  115. /**
  116. * 锤子厂商判断
  117. *
  118. * @return
  119. */
  120. public static boolean isSmartisan() {
  121. return Build.BRAND != null && Build.BRAND.toLowerCase().equals("smartisan");
  122. }
  123. /**
  124. * 跳转华为手机管家的启动管理页
  125. * 操作步骤:应用启动管理 -> 关闭应用开关 -> 打开允许自启动
  126. *
  127. * @param context
  128. */
  129. public static void goHuaweiSetting(Context context) {
  130. try {
  131. showActivity(context, "com.huawei.systemmanager",
  132. "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
  133. } catch (Exception e) {
  134. showActivity(context, "com.huawei.systemmanager",
  135. "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");
  136. }
  137. }
  138. /**
  139. * 跳转小米安全中心的自启动管理页面
  140. * 操作步骤:授权管理 -> 自启动管理 -> 允许应用自启动
  141. *
  142. * @param context
  143. */
  144. public static void goXiaomiSetting(Context context) {
  145. showActivity(context, "com.miui.securitycenter",
  146. "com.miui.permcenter.autostart.AutoStartManagementActivity");
  147. }
  148. /**
  149. * 跳转 OPPO 手机管家
  150. * 操作步骤:权限隐私 -> 自启动管理 -> 允许应用自启动
  151. *
  152. * @param context
  153. */
  154. public static void goOPPOSetting(Context context) {
  155. try {
  156. showActivity(context, "com.coloros.phonemanager");
  157. } catch (Exception e1) {
  158. try {
  159. showActivity(context, "com.oppo.safe");
  160. } catch (Exception e2) {
  161. try {
  162. showActivity(context, "com.coloros.oppoguardelf");
  163. } catch (Exception e3) {
  164. showActivity(context, "com.coloros.safecenter");
  165. }
  166. }
  167. }
  168. }
  169. /**
  170. * 跳转 VIVO 手机管家
  171. * 操作步骤:权限管理 -> 自启动 -> 允许应用自启动
  172. *
  173. * @param context
  174. */
  175. public static void goVIVOSetting(Context context) {
  176. showActivity(context, "com.iqoo.secure");
  177. }
  178. /**
  179. * 跳转魅族手机管家
  180. * 操作步骤:权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行
  181. *
  182. * @param context
  183. */
  184. public static void goMeizuSetting(Context context) {
  185. showActivity(context, "com.meizu.safe");
  186. }
  187. /**
  188. * 跳转三星智能管理器
  189. * 操作步骤:自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用
  190. *
  191. * @param context
  192. */
  193. public static void goSamsungSetting(Context context) {
  194. try {
  195. showActivity(context, "com.samsung.android.sm_cn");
  196. } catch (Exception e) {
  197. showActivity(context, "com.samsung.android.sm");
  198. }
  199. }
  200. /**
  201. * 跳转乐视手机管家
  202. * 操作步骤:自启动管理 -> 允许应用自启动
  203. *
  204. * @param context
  205. */
  206. public static void goLetvSetting(Context context) {
  207. showActivity(context, "com.letv.android.letvsafe",
  208. "com.letv.android.letvsafe.AutobootManageActivity");
  209. }
  210. /**
  211. * 跳转锤子手机管家
  212. * 操作步骤:自启动管理 -> 允许应用自启动
  213. *
  214. * @param context
  215. */
  216. public static void goSmartisanSetting(Context context) {
  217. showActivity(context, "com.smartisanos.security");
  218. }

华为跳转页面如下,然后手动打开自启动后台运行

这样操作下来,不是特殊情况下息屏后定时器就能持续运行了。


                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/889053
推荐阅读
相关标签
  

闽ICP备14008679号