前言###
adb是查看系統(tǒng)狀態(tài)一個(gè)很好的工具瘤缩,本文使用adb來解讀JobScheduler的狀態(tài)疲牵。JobScheduler是google為了省電而設(shè)計(jì)的一種用于調(diào)度工作的機(jī)制秧了,為了查看JobScheduler的運(yùn)行效果,除了實(shí)際觀察運(yùn)行狀態(tài)外侈离,adb也是一種很不錯(cuò)的輔助調(diào)試工具蹄葱。
目錄###
一.系統(tǒng)服務(wù)JobSchedulerService
二.注冊的job記錄文件jobs.xml
三.job參數(shù)解釋
一.系統(tǒng)服務(wù)JobSchedulerService#####
服務(wù)在SystemServer中注冊,屬于系統(tǒng)服務(wù)
源碼路徑:/frameworks/base/services/java/com/android/server/SystemServer.java
mSystemServiceManager.startService(JobSchedulerService.class);
執(zhí)行 adb shell service list查看系統(tǒng)服務(wù)究西,在列出的所有系統(tǒng)服務(wù)中可以找到
jobscheduler: [android.app.job.IJobScheduler]
二.注冊的job記錄文件jobs.xml#####
源碼路徑:/frameworks/base/services/core/java/com/android/server/job/JobStore.java
系統(tǒng)中的job記錄在文件jobs.xml窗慎,完整路徑是/data/system/job/jobs.xml,查看該文件手機(jī)要先root
private JobStore(Context context, File dataDir) {
mContext = context;
mDirtyOperations = 0;
File systemDir = new File(dataDir, "system");
File jobDir = new File(systemDir, "job");
jobDir.mkdirs();
mJobsFile = new AtomicFile(new File(jobDir, "jobs.xml"));
mJobSet = new ArraySet<JobStatus>();
readJobMapFromDisk(mJobSet);
}
cat命令查看該文件卤材,文件記錄著系統(tǒng)中此刻所有被調(diào)度的job
root@Xiaomi:/data/system/job # cat jobs.xml
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<job-info version="0">
<job jobid="137303610" package="com.google.android.apps.maps" class="com.google.android.apps.gmm.offline.OfflineAutoUpdateJobService" uid="10099">
<constraints connectivity="true" idle="true" charging="true" />
<periodic period="86400000" delay="1485554596322" />
<extras />
</job>
<job jobid="137303609" package="com.google.android.apps.maps" class="com.google.android.apps.gmm.offline.OfflineAutoUpdateJobService" uid="10099">
<constraints connectivity="true" idle="true" charging="true" />
<one-off delay="1485444550825" />
<extras />
</job>
<job jobid="1" package="com.example.wfcvs" class="com.example.wfcvs.wenfengService" uid="10146">
<constraints charging="true" />
<one-off delay="1485348887275" />
<extras />
</job>
<job jobid="20537" package="android" class="com.android.server.backup.KeyValueBackupJob" uid="1000">
<constraints connectivity="true" charging="true" />
<one-off deadline="1485591350955" delay="1485519812187" />
<extras />
</job>
<job jobid="808" package="android" class="com.android.server.MountServiceIdler" uid="1000">
<constraints idle="true" charging="true" />
<one-off delay="1485417599095" />
<extras />
</job>
</job-info>
三.job參數(shù)解釋#####
參數(shù) | 解釋 |
---|---|
jobid | 每個(gè)job的id遮斥,android.app.job.JobInfo.Builder.Builder(int jobId, ComponentName jobService) 指定 |
package | 包名 |
class | job的類名,這個(gè)類繼承于JobService |
uid | 應(yīng)用uid |
constraints | job被調(diào)度的條件 |
one-off | 跟時(shí)間相關(guān)扇丛,記錄job的deadline术吗,和延時(shí)執(zhí)行的時(shí)間,注意這個(gè)是時(shí)間點(diǎn)帆精,由函數(shù)systemclock.elapsedrealtime()生成较屿,即系統(tǒng)啟動(dòng)的時(shí)間材蹬,到這個(gè)時(shí)間點(diǎn)job會(huì)被觸發(fā) |
例如:
<constraints connectivity="true" charging="true" /> 代表網(wǎng)絡(luò)連接并且在充電狀態(tài)下,job會(huì)被調(diào)度
<one-off delay="1485348887275" /> 代表job在滿足條件的情況下吝镣,必須要等到1485348887275這個(gè)時(shí)間點(diǎn)才會(huì)被執(zhí)行
jobs.xml文件需要手機(jī)root情況下才能查看堤器,沒有root要怎么查看呢?當(dāng)然有方法末贾,執(zhí)行adb shell dumpsys jobscheduler
adb shell dumpsys jobscheduler
Started users: u0
Registered jobs:
117..:[ComponentInfo{com.google.android.apps.maps/com.google.android.apps.gmm.offline.OfflineAutoUpdateJobService},jId=137303610,u0,R=(-172:44:27,none),N=1,C=true,I=true,F=1,P=true,ANI=true]
148..:[ComponentInfo{com.google.android.apps.maps/com.google.android.apps.gmm.offline.OfflineAutoUpdateJobService},jId=137303609,u0,R=(-203:18:32,none),N=1,C=true,I=true,F=1,P=true,ANI=true]
849..:[ComponentInfo{com.example.wfcvs/com.example.wfcvs.wenfengService},jId=1,u0,R=(56:07,none),N=0,C=true,I=false,F=8,P=true,ANI=true]
246..:[ComponentInfo{android/com.android.server.MountServiceIdler},jId=808,u0,R=(-210:47:44,none),N=0,C=true,I=true,F=0,P=false,ANI=true]
Conn.
connected: true unmetered: true
148..: C=true, UM=false
117..: C=true, UM=false
Alarms (843148174)
Next delay alarm in 3367s
Next deadline alarm in 9223372036011627s
Tracking:
849..: (846515518, N/A)
Idle: false
3
246..
148..
117..
Batt.
Stable power: true
246885848,14863036,11761044,84957259
AppIdle
Parole On: false
android:idle=false, com.google.android.apps.maps:idle=false, com.google.android.apps.maps:idle=false, com.example.wfcvs:idle=false,
Pending:
Active jobs:
mReadyToRock=true
mDeviceIdleMode=false
例如
849..:[ComponentInfo{com.example.wfcvs/com.example.wfcvs.wenfengService},jId=1,u0,R=(56:07,none),N=0,C=true,I=false,F=8,P=true,ANI=true]
849 代表這個(gè)job的hashcode的前三位
R 代表延時(shí)時(shí)間和deadline闸溃,(56:07,none)代表延時(shí)到56:07才能執(zhí)行,deadline為none代表沒有deadline拱撵。
N 代表網(wǎng)絡(luò)
C 代表充電
I 代表idle
F 代表調(diào)度失敗的次數(shù)
P 代表job是否Persist
好了辉川,伙伴們,使用你們的adb工具感受下吧拴测!新年快樂E移臁!