firebase作业调度程序在oreo设备上不工作

mzaanser  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(279)

我正在使用firebase作业调度器api在后台运行服务。在棉花糖,它是工作良好,但当来到奥利奥设备,它不工作。它显示错误或警告信息,如
2019-02-04 14:20:22.285 2525-5699/? e/networkscheduler:正在删除任务,因为应用程序的play services sdk版本不支持android o。更新sdk或降低应用程序的目标sdk版本。包:com.webappclouds.jobserviceexample
我已经在git hub中完成了firebase job dispatcher api步骤中提到的内容。
主活动.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;
}
}

生成服务.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'我更新了它,它从现在开始工作

sg2wtvxw

sg2wtvxw1#

错误消息实际上有解决方案
更新sdk或降低应用程序的目标sdk版本。包:com.webappclouds.jobserviceexample
从更新库 0.5.2 ```
implementation 'com.firebase:firebase-jobdispatcher:0.8.5'

我还注意到您的清单上有一个空的权限值,请删除它。

android:permission=""

如果有帮助,请告诉我?
我建议你迁移到 `WorkManager` 因为它是一个更向后兼容和更简单的api。使用此库时,所有api级别的限制都会得到处理(限制与安全性不同)。
您可以在这里找到一个简单的工作流示例:
https://developer.android.com/topic/libraries/architecture/workmanager/basics#java
以及文件:
https://developer.android.com/topic/libraries/architecture/workmanager/
我建议你两个都读。
android oreo后台执行限制:https://www.youtube.com/watch?v=pumf_4yjtmc
n9vozmp4

n9vozmp42#

请在此处查看有关firebase jobdispatcher的官方android文档:
workmanager是一个库,用于在android中调度和执行可延迟的后台工作。建议更换firebase jobdispatcher。以下指南将引导您完成将firebase jobspatcher实现迁移到workmanager的过程。

相关问题