Android 26对后台service进行了限制,但是也引入了JobIntentService来取代之前的service
JobIntentService和IntentService功能基本相同
看下JobIntentService的官方文档
---------------------
Helper for processing work that has been enqueued for a job/service. When running on Android O
or later, the work will be dispatched as a job via JobScheduler.enqueue
. When running on older versions of the platform, it will useContext.startService
.
You must publish your subclass in your manifest for the system to interact with. This should be published as a JobService
, as described for that class, since on O and later platforms it will be executed that way.
Use enqueueWork(Context, Class, int, Intent)
to enqueue new work to be dispatched to and handled by your service. It will be executed in onHandleWork(Intent)
.
------------------
前面都是废话
You do not need to use WakefulBroadcastReceiver
when using this class. When running on Android O
, the JobScheduler will take care of wake locks for you (holding a wake lock from the time you enqueue work until the job has been dispatched and while it is running). When running on previous versions of the platform, this wake lock handling is emulated in the class here by directly calling the PowerManager; this means the application must request theWAKE_LOCK
permission.
使用这个类则不需要用WakefulBroadcastReceive,在android O, JobScheduler 会帮你处理好wake locks。
在之前的版本,直接调用PowerManager,所以主要要申请WAKE_LOCK权限
There are a few important differences in behavior when running on Android O
or later as a Job vs. pre-O:
android o 之前和之后有些不同
When running as a pre-O service, the act of enqueueing work will generally start the service immediately, regardless of whether the device is dozing or in other conditions. When running as a Job, it will be subject to standard JobScheduler policies for a Job with a setOverrideDeadline(long)
of 0: the job will not run while the device is dozing, it may get delayed more than a service if the device is under strong memory pressure with lots of demand to run jobs.
android o 之前会立即执行,android o之后可能会延迟。
When running as a pre-O service, the normal service execution semantics apply: the service can run indefinitely, though the longer it runs the more likely the system will be to outright kill its process, and under memory pressure one should expect the process to be killed even of recently started services. When running as a Job, the typical JobService
execution time limit will apply, after which the job will be stopped (cleanly, not by killing the process) and rescheduled to continue its execution later. Job are generally not killed when the system is under memory pressure, since the number of concurrent jobs is adjusted based on the memory state of the device.
在android o 之前,service可能会被系统kill ,android o 之后,JobService 不会。
实现如下:
Here is an example implementation of this class:
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.JobIntentService;
import android.util.Log;
import android.widget.Toast;
/**
* Example implementation of a JobIntentService.
*/
public class SimpleJobIntentService extends JobIntentService {
/**
* Unique job ID for this service.
*/
static final int JOB_ID = 1000;
/**
* Convenience method for enqueuing work in to this service.
*/
static void enqueueWork(Context context, Intent work) {
enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(Intent intent) {
// We have received work to do. The system or framework is already
// holding a wake lock for us at this point, so we can just go.
Log.i("SimpleJobIntentService", "Executing work: " + intent);
String label = intent.getStringExtra("label");
if (label == null) {
label = intent.toString();
}
toast("Executing: " + label);
for (int i = 0; i < 5; i++) {
Log.i("SimpleJobIntentService", "Running service " + (i + 1)
+ "/5 @ " + SystemClock.elapsedRealtime());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime());
}
@Override
public void onDestroy() {
super.onDestroy();
toast("All work complete");
}
final Handler mHandler = new Handler();
// Helper for showing tests
void toast(final CharSequence text) {
mHandler.post(new Runnable() {
@Override public void run() {
Toast.makeText(SimpleJobIntentService.this, text, Toast.LENGTH_SHORT).show();
}
});
}
}
==================================================
上面是google官方文档的原文,下面试下JobIntentService好不好用
TestService类:
public class TestService extends JobIntentService {
private static final String TAG = "TestService";
@Override
public void onCreate() {
Log.i(TAG,"onCreate");
super.onCreate();
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.i(TAG,"onHandleWork"+intent.getStringExtra("intent"));
}
}
调用代码:
Intent intent =new Intent();
intent.putExtra("intent","haha");
TestService.enqueueWork(this,TestService.class,1,intent);
打印:
2019-05-25 23:43:38.205 30490-30490/calc.superdy.ttest I/TestService: onCreate
2019-05-25 23:43:38.233 30490-30573/calc.superdy.ttest I/TestService: onHandleWorkhaha