前言
前面文章咱们封装了网络恳求组件lib_nework
和图片加载组件lib_image_loader
,今天咱们来封装一个进程保活的组件lib_电池修复pull_alive
相关文章:
Android组件化开发(一)–Mavegithub官网n私服的建立
Andro肉跳测吉凶id组件化开发(二)–网络恳求组件封装
Android组件化开发(三)–图片人体承受的最大电压加载组件封装

远古时代,出现过许多人体承受的最大电压黑科技,比如MarsDaemogithub直播平台永久回家n
,运用双进程看github是什么护的方法进行保活,在当时可谓风景无限电池型号5号和7号,惋惜在8.0
时代到来就被人头攒动废弃了。
又比如后边出现的1像素Activity
的保活方法,说他github开放私库流氓一点不过火,假如每个人都运用这些操作,由于功耗的大大添加,会直接影响到手机运用寿命。
因而各大手机厂商为了处理这一现象的发生,在体系层面对这些流氓行为做出了约束:
后台进程即使你是要黑科技让进程优先级很高,也或许被杀死
,所以我甘愿称是运用的求生
而不是保活
。
这种方法初衷是好的,降低了设备功耗,降低了内存,防止手机发烫等,但是关于一些真实需求做保活操作的运用来让天秤倒追的星座说,可谓苦不堪言。
于是乎新型的求生
措施又出现了。
怎样优雅的进行求生
Android6.0
以后GitHub体系推出了一个电池优化计划,对一些高耗让天秤倒追的星座电的进程会进行战略杀死。
那或许有人要问了优先级是什么意思,微信和qq这些高耗电运用是怎样做到保存的呢?
看下图

能够看到体系将电池健康度怎么看微信和qq都放到了他们的白名单
里边了,这个是怎样做到的呢?
其实, 这个是微信和厂商做了洽谈,将他们自己的运用设置到了他们的电量优化白名单闰土刺猹中。 下次产品再问我为啥他们能够做到的时候,我就把这张图甩给他们。。
那咱们可不能够也让厂商给咱们加名单呢电池型号5号和7号?呃呃呃。。

好在手机厂商没有把路堵死,给咱们留了一条后路
- 1.首要运用下面的代码检测咱们进程是否在白名单中初始化:
@RequiresApi(api = Build.VERSION_CODES.M) private boolean isIgnoringBatteryOptimizations() { boolean isIgnoring = false; PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); if (powerManager != null) { isIgnoring = powerManager.isIgnoringBatteryOptimizations(getPackageName()); } return isIgnoring; }
- 2.假如没有:调用下面的代码申请加入白名单
@RequiresApi(api = Build.VERSION_CODES.M) public void requestIgnoreBatteryOptimizations() { try { Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); } catch (Exception e) { e.printStackTrace(); } }
申请时会弹出一个让用户挑选的Dialog:

窗口中会提示该操作或GitHub许是影响电池的运用,假如需求监听用户的按键,能够运用startActivityResult
在onActivityResult
中监听
好了,白名单是加好了,那是不是便是万事GitHub大吉了初始化是什么意思呢?让天秤倒追的星座
手机厂商:哪有那么简单,就算你加入了电量优化白名单,你要是不按规矩来,在后台运转的初始化sdk什么意思进程仍是会被咱们杀掉?还有啥招式快快使出来吧

咱们知道进程被杀电池种类死,是由于体系的后台管理初始化sdk什么意思体系把咱们重启的路堵住了,为啥堵我啊?按我说或许体系看你这个进程不顺眼吧,哈哈。。
言归正传:
其实是你不在后台管理的自发动白名单
中,自发动白名单就像一张通行证
,GitHub你的运用需求在体系后台自发动,就要在白名单上,不然哪里闰土刺猹来回哪里去吧
那白名单这么好,怎样才能加入TM呢?
要知道市面上手机厂家许多,每个厂RTC家的体系都不相同
,一个体系还有许多乃至几十个版别
,这让咱们怎样加入啊?
而大部分自发动操作能够在厂商的手机管家
的设置里边设置:
最理想的做法
:咱们根据不优先级调度算法同手机,乃至是不同的体系版别,给用户出现一个图文操作过程,而且供给一个按钮,直接跳转到指定页面进行设置
- 咱们先定义下面两个方法:
/** * 跳转到指定运用的主页 */ private static void showActivity(Context context,@NonNull String packageName) { Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); context.startActivity(intent); } /** * 跳转到指定运用的指定页面 */ private 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); }
- 手机厂商判断:
华为: public boolean isHuawei() { if (Build.BRAND == null) { return false; } else { return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor"); } } 小米 public static boolean isXiaomi() { return Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi"); } OPPO public static boolean isOPPO() { return Build.BRAND != null && Build.BRAND.toLowerCase().equals("oppo"); } VIVO public static boolean isVIVO() { return Build.BRAND != null && Build.BRAND.toLowerCase().equals("vivo"); } 魅族 public static boolean isMeizu() { return Build.BRAND != null && Build.BRAND.toLowerCase().equals("meizu"); } 三星 public static boolean isSamsung() { return Build.BRAND != null && Build.BRAND.toLowerCase().equals("samsung"); }
- 手机管家或许自发动界面发动方法:
华为: private void goHuaweiSetting() { try { showActivity("com.huawei.systemmanager", "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity"); } catch (Exception e) { showActivity("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.bootstart.BootStartActivity"); } } 小米: private void goXiaomiSetting() { showActivity("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"); } OPPO: private void goOPPOSetting() { try { showActivity("com.coloros.phonemanager"); } catch (Exception e1) { try { showActivity("com.oppo.safe"); } catch (Exception e2) { try { showActivity("com.coloros.oppoguardelf"); } catch (Exception e3) { showActivity("com.coloros.safecenter"); } } } } VIVO private void goVIVOSetting() { showActivity("com.iqoo.secure"); } 魅族: private void goMeizuSetting() { showActivity("com.meizu.safe"); } 三星: private void goSamsungSetting() { try { showActivity("com.samsung.android.sm_cn"); } catch (Exception e) { showActivity("com.samsung.android.sm"); } }
总结下上面咱们所讲:
- 1.为了不被电量优化,咱们需求将运用添加进电量优化白名单中
- 2.为了能够在被杀身后,自己能够发动自己,需求将运用自发动开关开启,能够使
用图文引人头攒动的近义词导的方法:
参考下RTC面这张图:

保活增强:
咱们都知优先级调度算法道保活操作一般是运用一个前台服务来挂起咱们的运用
:
还有的保活操作是运用一个Job优先级英文Service
来对让体github汤姆系在某个条件契合下回调一个恳求操作。
根据以上剖析:
- 笔者这边封装了一个保活组件-
lib_pull_alive
:
结合了:前台服务
+JobService
+初始化电脑的后果电量优化白名单
+引导用户运优先级c语言用自发动的方法
完成了一个求生
计划,代码如下:
keepAliveService.java package com.anna.lib_keepalive.service; import android.app.job.JobInfo; import android.app.job.JobParameters; import android.app.job.JobScheduler; import android.app.job.JobService; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Handler; import android.os.Message; import android.util.Log; import androidx.annotation.RequiresApi; import com.anna.lib_keepalive.forground.ForgroundNF; import com.anna.lib_keepalive.utils.Utils; /** * 创立一个JobService用于提高运用优先级 */ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class KeepAliveService extends JobService { private static final String TAG = KeepAliveService.class.getSimpleName(); private JobScheduler mJobScheduler; private static final int JOB_ID = 1; private ComponentName JOB_PG; private int NOTIFICATION_ID = 10; private ForgroundNF mForgroundNF; private Handler mJobHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { Log.d(TAG, "pull alive."); jobFinished((JobParameters) msg.obj, true); return true; } }); @Override public void onCreate() { super.onCreate(); mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); JOB_PG = new ComponentName(getPackageName(),KeepAliveService.class.getName()); mForgroundNF = new ForgroundNF(this); Utils.requestIgnoreBatteryOptimizations(this); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void start(Context context){ Intent intent = new Intent(context,KeepAliveService.class); context.startService(intent); } @Override public boolean onStartJob(JobParameters params) { Log.d(TAG,"onStartJob"); mJobHandler.sendMessage(Message.obtain(mJobHandler, 1, params)); return true; } /**体系回调运用,阐明触发了job条件 * @param params * @return */ @Override public boolean onStopJob(JobParameters params) { Log.d(TAG,"onStopJob"); mJobHandler.sendEmptyMessage(1); return false; } @Override public int onStartCommand(Intent intent, int flags, int startId) { JobInfo job = initJob(); mJobScheduler.schedule(job); startNotificationForGround(); return START_STICKY; } /** * 大于18能够运用一个取消Notification的服务 */ private void startNotificationForGround(){ if(Build.VERSION.SDK_INT<18){ mForgroundNF.startForegroundNotification(); }else{ mForgroundNF.startForegroundNotification(); Intent it = new Intent(this, CancelNotifyervice.class); startService(it); } } /**初始化Job使命 * @return */ private JobInfo initJob() { JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, JOB_PG); if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){ builder.setMinimumLatency(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); //执行的最小延迟时间 builder.setOverrideDeadline(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); //执行的最长延时时间 builder.setBackoffCriteria(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS, JobInfo.BACKOFF_POLICY_LINEAR);//线性重试计划 }else { builder.setPeriodic(JobInfo.DEFAULT_INITIAL_BACKOFF_MILLIS); } builder.setPersisted(false); builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE); builder.setRequiresCharging(false); return builder.build(); } @Override public void onDestroy() { super.onDestroy(); mForgroundNF.stopForegroundNotification(); } }
ForgroundNF.java package com.anna.lib_keepalive.forground; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.Service; import android.content.Context; import android.os.Build; import androidx.core.app.NotificationCompat; import com.anna.lib_keepalive.R; public class ForgroundNF { private static final int START_ID = 101; private static final String CHANNEL_ID = "app_foreground_service"; private static final String CHANNEL_NAME = "前台保活服务"; private Service service; private NotificationManager notificationManager; private NotificationCompat.Builder mNotificationCompatBuilder; public ForgroundNF(Service service){ this.service = service; initNotificationManager(); initCompatBuilder(); } /** * 初始化NotificationCompat.Builder */ private void initCompatBuilder() { mNotificationCompatBuilder = new NotificationCompat.Builder(service,CHANNEL_ID); //标题 mNotificationCompatBuilder.setContentTitle("test keep alive"); //告诉内容 mNotificationCompatBuilder.setContentText("test alive"); mNotificationCompatBuilder.setSmallIcon(R.mipmap.ic_launcher_round); } /** * 初始化notificationManager并创立NotificationChannel */ private void initNotificationManager(){ notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE); //针对8.0+体系 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NotificationManager.IMPORTANCE_LOW); channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC); channel.setShowBadge(false); notificationManager.createNotificationChannel(channel); } } public void startForegroundNotification(){ service.startForeground(START_ID,mNotificationCompatBuilder.build()); } public void stopForegroundNotification(){ notificationManager.cancelAll(); service.stopForeground(true); } }
完好代码能够查看github上的Demo: github.com/ByteYuhb/an…
总结:
本文是组件化开发的第四篇,也是第三个功用组件的封装,都已上传到Gi优先级调度算法thub,后期会连续引荐其他组件优先级c语言的封装。

评论(0)