Gud*_*all 0 android firebase android-intentservice android-jobscheduler
我正在尝试启动一个IntentService来注册Android O上的firebase云消息.
在Android O上,不允许"在不允许的情况下"启动Intent Service,并且每个人都告诉我使用JobService而不是如何使用它.
JobInfo.Builder应该有什么限制才能有"允许它的情况",我一直得到相同的IllegalStateException
这是我的JobService
@Override
public boolean onStartJob(JobParameters params) {
Intent intent = new Intent(this, RegistrationIntentService.class);
getApplicationContext().startService(intent);
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
public static void scheduleJob(Context context) {
ComponentName serviceComponent = new ComponentName(context, MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(MyJobService.JOB_ID, serviceComponent);
builder.setMinimumLatency(1 * 1000); // wait at least
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
if(jobScheduler != null) jobScheduler.schedule(builder.build());
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是支持库版本26.1.0或更高版本,则可以访问JobIntentService类似于Intent Service具有作业调度程序附加优势的支持库,除了启动它之外,您无需管理任何其他内容.
根据文件
帮助处理已经为工作/服务排队的工作.在Android O或更高版本上运行时,工作将通过JobScheduler.enqueue作为作业分派.在旧版本的平台上运行时,它将使用Context.startService.
您可以在JobIntentService中找到更多详细信息.
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.JobIntentService;
public class JobIntentNotificationService extends JobIntentService {
public static void start(Context context) {
Intent starter = new Intent(context, JobIntentNotificationService.class);
JobIntentNotificationService.enqueueWork(context, starter);
}
/**
* Unique job ID for this service.
*/
static final int JOB_ID = 1000;
/**
* Convenience method for enqueuing work in to this service.
*/
private static void enqueueWork(Context context, Intent intent) {
enqueueWork(context, JobIntentNotificationService.class, JOB_ID, intent);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
// do your work here
}
}
Run Code Online (Sandbox Code Playgroud)
你打电话的方式是
JobIntentNotificationService.start(getApplicationContext());
Run Code Online (Sandbox Code Playgroud)
您需要为Oreo之前的设备添加此权限
<!-- used for job scheduler pre Oreo -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4069 次 |
| 最近记录: |