日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 进程保活】应用进程拉活 ( 双进程守护 + JobScheduler 保活 | 成功率最高 | 推荐使用 )

發布時間:2025/6/17 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 进程保活】应用进程拉活 ( 双进程守护 + JobScheduler 保活 | 成功率最高 | 推荐使用 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、 雙進程守護保活 + JobScheduler 原理
  • 二、 雙進程守護保活 + JobScheduler 源碼
    • 1、JobService 代碼
    • 2、判定服務運行工具類
    • 3、清單文件
    • 4、MainActivity 代碼
    • 5、運行效果
  • 三、 源碼資源





一、 雙進程守護保活 + JobScheduler 原理



【Android 進程保活】應用進程拉活 ( JobScheduler 拉活 | JobScheduler 使用流程 | JobService 服務 | 不同版本兼容 | 源碼資源 ) 博客中介紹了 JobScheduler 的用法 ;

【Android 進程保活】應用進程拉活 ( 雙進程守護保活 ) 博客中介紹了雙進程守護保活用法 ;

使用 " 雙進程守護保活 + JobScheduler " 機制 , 成功率最高 ;


" 雙進程守護保活 + JobScheduler " 整合方法 :

在 JobService 的 onStartJob 方法中 , 判定 " 雙進程守護保活 " 中的雙進程是否掛了 , 如果這兩個進程掛了 , 就重新將掛掉的進程重啟 ;

判定 Service 進程是否運行 :

public static boolean isServiceRunning(Context context, String serviceName){if(TextUtils.isEmpty(serviceName)) return false;ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);// 最多獲取 200 個正在運行的 ServiceList<ActivityManager.RunningServiceInfo> infos =activityManager.getRunningServices(200);// 遍歷當前運行的 Service 信息, 如果找到相同名稱的服務 , 說明某進程正在運行for (ActivityManager.RunningServiceInfo info: infos){if (TextUtils.equals(info.service.getClassName(), serviceName)){return true;}}return false;}



二、 雙進程守護保活 + JobScheduler 源碼



大部分代碼與 【Android 進程保活】應用進程拉活 ( 雙進程守護保活 ) 博客中重復 , 這里只貼出 JobScheduler 相關源碼 ;



1、JobService 代碼


package kim.hsl.two_progress_alive;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.util.Log;import androidx.annotation.RequiresApi;@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class KeepAliveJobService extends JobService {@Overridepublic boolean onStartJob(JobParameters params) {Log.i("KeepAliveJobService", "JobService onStartJob 開啟");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){// 如果當前設備大于 7.0 , 延遲 5 秒 , 再次執行一次startJob(this);}// 判定本地前臺進程是否正在運行boolean isLocalServiceRunning =ServiceUtils.isServiceRunning(this, LocalForegroundService.class.getName());if (!isLocalServiceRunning){startService(new Intent(this, LocalForegroundService.class));}// 判定遠程前臺進程是否正在運行boolean isRemoteServiceRunning =ServiceUtils.isServiceRunning(this, RemoteForegroundService.class.getName());if (!isRemoteServiceRunning){startService(new Intent(this, RemoteForegroundService.class));}return false;}@Overridepublic boolean onStopJob(JobParameters params) {Log.i("KeepAliveJobService", "JobService onStopJob 關閉");return false;}public static void startJob(Context context){// 創建 JobSchedulerJobScheduler jobScheduler =(JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);// 第一個參數指定任務 ID// 第二個參數指定任務在哪個組件中執行// setPersisted 方法需要 android.permission.RECEIVE_BOOT_COMPLETED 權限// setPersisted 方法作用是設備重啟后 , 依然執行 JobScheduler 定時任務JobInfo.Builder jobInfoBuilder = new JobInfo.Builder(10,new ComponentName(context.getPackageName(), KeepAliveJobService.class.getName())).setPersisted(true);// 7.0 以下的版本, 可以每隔 5000 毫秒執行一次任務if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){jobInfoBuilder.setPeriodic(5_000);}else{// 7.0 以上的版本 , 設置延遲 5 秒執行// 該時間不能小于 JobInfo.getMinLatencyMillis 方法獲取的最小值jobInfoBuilder.setMinimumLatency(5_000);}// 開啟定時任務jobScheduler.schedule(jobInfoBuilder.build());} }

2、判定服務運行工具類


package kim.hsl.two_progress_alive;import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.text.TextUtils;import org.w3c.dom.Text;import java.util.List;public class ServiceUtils {/*** 判定 Service 是否在運行* @param context* @return*/public static boolean isServiceRunning(Context context, String serviceName){if(TextUtils.isEmpty(serviceName)) return false;ActivityManager activityManager =(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);// 最多獲取 200 個正在運行的 ServiceList<ActivityManager.RunningServiceInfo> infos =activityManager.getRunningServices(200);// 遍歷當前運行的 Service 信息, 如果找到相同名稱的服務 , 說明某進程正在運行for (ActivityManager.RunningServiceInfo info: infos){if (TextUtils.equals(info.service.getClassName(), serviceName)){return true;}}return false;} }

3、清單文件


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="kim.hsl.two_progress_alive"><uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Two_Progress_Alive"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 本地提權前臺服務 Service --><serviceandroid:name=".LocalForegroundService"android:enabled="true"android:exported="true"></service><!-- 本地服務 , API 18 ~ 25 以上的設備, 關閉通知到專用服務 --><serviceandroid:name=".LocalForegroundService$CancelNotificationService"android:enabled="true"android:exported="true"></service><!-- 遠程提權前臺服務 Service --><serviceandroid:name=".RemoteForegroundService"android:enabled="true"android:exported="true"android:process=":remote"></service><!-- 遠程服務 , API 18 ~ 25 以上的設備, 關閉通知到專用服務 --><serviceandroid:name=".RemoteForegroundService$CancelNotificationService"android:enabled="true"android:exported="true"android:process=":remote"></service><!-- JobScheduler 拉活 --><serviceandroid:name=".KeepAliveJobService"android:enabled="true"android:exported="true"android:permission="android.permission.BIND_JOB_SERVICE"></service></application></manifest>

4、MainActivity 代碼


package kim.hsl.two_progress_alive;import android.content.Intent; import android.os.Build; import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 通過前臺 Service 提升應用權限// 啟動普通 Service , 但是在該 Service 的 onCreate 方法中執行了 startForeground// 變成了前臺 Service 服務startService(new Intent(this, LocalForegroundService.class));startService(new Intent(this, RemoteForegroundService.class));// JobScheduler 拉活if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {KeepAliveJobService.startJob(this);}} }

5、運行效果


運行后 , 兩個進程成功運行 ;

即使將啟動雙進程的代碼注釋掉 , 也可以成功拉起雙進程 ;





三、 源碼資源



源碼資源 :

  • GitHub 地址 : https://github.com/han1202012/Two_Progress_Alive
  • CSDN 源碼快照 : https://download.csdn.net/download/han1202012/16623594

總結

以上是生活随笔為你收集整理的【Android 进程保活】应用进程拉活 ( 双进程守护 + JobScheduler 保活 | 成功率最高 | 推荐使用 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。