在我的实时设备上运行代码时,Firebase作业调度程序不会触发我的JobService类,为什么?

时间:2018-04-11 20:09:24

标签: android firebase google-play-services firebase-job-dispatcher jobservice

我正在开发一个项目,该项目使用firebase作业调度程序来安排间隔运行的作业。但问题是,我在Android工作室模拟器上运行代码,它运行得很好。这份工作已经安排好了,我收到了服务类的祝酒词,显示代码的执行是成功的,但我尝试在实时的Android手机上运行它,代码在设备上构建并运行,但是工作从未安排过,没有收到任何消息。我尝试了所有的东西,而且还没有调用jobService类。我尝试在模拟器上再次运行应用程序,它工作正常,但它从未在我的实时设备上工作。请问这可能是什么原因。我将firebase gradle依赖项添加到build.gradle文件中。我将jobService添加到了manifest文件夹中。我不仅知道造成这种困境的原因是什么。我需要一个解释。感谢。

MainActivity

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import static android.R.attr.start;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button start_job, stop_job;
    private static final String Job_Tag = "myJobTag";
    private FirebaseJobDispatcher jobDispatcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        jobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));

        start_job = (Button) findViewById(R.id.start_job);
        stop_job = (Button) findViewById(R.id.stop_job);

        start_job.setOnClickListener(this);
        stop_job.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.start_job){

            Job job = jobDispatcher.newJobBuilder()
                    .setService(MyFirebaseJobScheduler.class)
                    .setLifetime(Lifetime.FOREVER)
                    .setRecurring(true)
                    .setTag(Job_Tag)
                    .setTrigger(Trigger.executionWindow(1, 2))
                    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
                    .setReplaceCurrent(false)
                    .setConstraints(Constraint.ON_ANY_NETWORK)
                    .build();
            jobDispatcher.mustSchedule(job);
            Toast.makeText(this, "Job scheduled.", Toast.LENGTH_SHORT).show();

        }else if(v.getId() == R.id.stop_job){
            jobDispatcher.cancel(Job_Tag);
            Toast.makeText(this, "Job canceled.", Toast.LENGTH_SHORT).show();
        }
    }
}

JobService类

import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;

public class MyFirebaseJobScheduler extends JobService {

    BackgroundScheduledTask backgroundScheduledTask;

    @Override
    public boolean onStartJob(final JobParameters jobParameters) {
        Log.d("JobDispatcher", "Job Called");
        backgroundScheduledTask = new BackgroundScheduledTask(){
            @Override
            protected void onPostExecute(String s) {
                Log.d("JobDispatcher", "PostExecute");
                Toast.makeText(getApplicationContext(), "Message from back task = "+s, Toast.LENGTH_LONG).show();
                jobFinished(jobParameters, false);
            }
        };
        backgroundScheduledTask.execute();
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        return false;
    }

    public static class BackgroundScheduledTask extends AsyncTask<Void, Void, String>{

        @Override
        protected String doInBackground(Void... params) {
            Log.d("JobDispatcher", "In background");
            return "Hello from background Job";
        }
    }
}

清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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:name=".MyFirebaseJobScheduler"
        android:exported="false">
        <intent-filter>
            <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
        </intent-filter>
    </service>
</application>

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "26.0.2"

    defaultConfig {
        applicationId "com.cyclon.server"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.firebase:firebase-jobdispatcher:0.5.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
}

2 个答案:

答案 0 :(得分:1)

您已设置 .setConstraints(Constraint.ON_ANY_NETWORK),表示需要网络连接。如果您想在没有Internet连接的情况下启动服务,那么您可以在安排作业时删除.setConstraints(Constraint.ON_ANY_NETWORK)

答案 1 :(得分:0)

如果它对某人有用,除了下面的所有其他先前的答案,我报告我所经历的:对于相同的问题,具有完全相同的代码结构,在我的情况下,它是通过更改来解决的在build.gradle中导入的firebase lib版本为最新版本。 我有: 实施'com.firebase:firebase-jobdispatcher:0.5.0' 然后我把: 实施'com.firebase:firebase-jobdispatcher:0.8.5' 也许更新的版本会一样。