我尝试在Android上与JobScheduler和JobService合作
我的安装是RPI(Raspberry PI)运行IOT RPI3 1.0.2
这是我的简单代码:
package com.mystuff.jobservicetest;
import android.app.Activity;
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.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JobScheduler jobScheduler = (JobScheduler) getSystemService(
Context.JOB_SCHEDULER_SERVICE);
ComponentName name = new ComponentName(this, JobServiceTest.class);
JobInfo jobInfo = new JobInfo.Builder(1,name).setPeriodic(1000).build();
int result = jobScheduler.schedule(jobInfo);
Log.d(TAG, "result = "+result);
}
public class JobServiceTest extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.d(TAG, "Service job started");
return false;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
return false;
}
}
}
androidmanifest.xml看起来很简单,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mystuff.jobservicetest">
<application>
<uses-library android:name="com.google.android.things" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.IOT_LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".MainActivity$JobServiceTest"
android:permission="android.permission.BIND_JOB_SERVICE"/>
</application>
</manifest>
logcat的结果是:
w/jobinfo:1的指定间隔为+1毫秒。夹持到+15毫秒
1的规定弯曲度为+1S0ms。夹紧至+5M0S0ms
d/mainActivity:结果=1
我缺少“服务作业已启动”日志。似乎没有人打电话给OnStartJob。
有什么提示吗?
谢谢