我正在使用Firebase作业调度程序api在后台运行服务。在棉花糖中,它可以正常工作,但是在使用Oreo设备时却无法正常工作。它显示错误或警告消息,例如
2019-02-04 14:20:22.285 2525-5699 /? E / NetworkScheduler:删除任务 应用的Play服务SDK版本不支持AndroidO。 更新SDK或降低应用程序的目标SDK版本。包: com.webappclouds.jobserviceexample
我已经完成了git hub中的firebase作业调度程序api步骤中提到的内容。
MainActiviy.java
package com.webappclouds.jobserviceexample;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.firebase.jobdispatcher.Constraint;
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;
public class MainActivity extends AppCompatActivity {
String TAG="MAIN ACTIVITY";
FirebaseJobDispatcher dispatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
}
public void startjob(View view) {
Log.d(TAG, "startjob: ");
Job job = createJob(dispatcher);
dispatcher.mustSchedule(job);
}
public void stopjob(View view) {
Log.d(TAG, "cancelJob: ");
dispatcher.cancelAll();
}
public Job createJob(FirebaseJobDispatcher dispatcher) {
Log.d(TAG, "createJob: ");
Job job = dispatcher.newJobBuilder()
// persist the task across boots
.setLifetime(Lifetime.FOREVER)
// Call this service when the criteria are met.
.setService(MakeService.class)
// unique id of the task
.setTag("LocationJob")
.setReplaceCurrent(true)
// We are mentioning that the job is not periodic.
.setRecurring(true)
// Run between 30 - 60 seconds from now.
.setTrigger(Trigger.executionWindow(0, 0))
.setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
// .setTrigger(Trigger.NOW)
//Run this job only when the network is avaiable.
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
return job;
}
}
MakeService.java
package com.webappclouds.jobserviceexample;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import com.firebase.jobdispatcher.JobService;
import java.util.logging.LogRecord;
import static android.content.ContentValues.TAG;
public class MakeService extends JobService {
boolean jobintentService=false;
Handler handler;
@Override
public void onCreate() {
super.onCreate();
handler= new Handler();
}
@Override
public boolean onStartJob(com.firebase.jobdispatcher.JobParameters job) {
Log.d(TAG, "onStartJob: ");
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run: Handler");
handler.postDelayed(this,10000);
}
},10000);
return true;
}
@Override
public boolean onStopJob(com.firebase.jobdispatcher.JobParameters job) {
Log.d(TAG, "onStopJob: ");
return true;
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webappclouds.jobserviceexample">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:exported="false"
android:permission=""
android:name=".MakeService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
</application>
</manifest>
构建(项目)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
构建(应用)
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.webappclouds.jobserviceexample"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.firebase:firebase-jobdispatcher:0.5.2'
}
我希望它能正常工作,但我不知道发生了什么。
**解决方案** 这是由于版本 实施'com.firebase:firebase-jobdispatcher:0.8.5' 我更新了它,从现在开始工作
答案 0 :(得分:0)
错误消息确实可以解决问题
要么更新SDK,要么降低应用程序的目标SDK版本。包: com.webappclouds.jobserviceexample
从0.5.2
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'
我还注意到您的清单上有一个空的权限值,请将其删除。
android:permission=""
让我知道这是否有帮助
我建议您迁移到WorkManager
,因为它是向后兼容和更简单的API。使用此库 all 时,您的API级别限制会得到解决(限制与安全性不同)。
您可以在此处找到一个简单的工作流程示例:
文档:
我建议您同时阅读两者。
答案 1 :(得分:0)
请查看有关Firebase JobDispatcher here的官方Android文档:
WorkManager是用于调度和执行可延期的库 Android中的后台工作。这是推荐的替代品 Firebase JobDispatcher。以下指南将引导您完成 将Firebase JobDispatcher实施迁移到的过程 工作管理器。