我正在开发一个项目,该项目使用 firebase 作业调度程序来安排间隔运行的作业。但问题是,我在 android studio 模拟器上运行了代码,它运行得很好。作业已安排好,我收到了来自服务类的祝酒词,表明代码执行成功,但是我尝试在实时 android 手机上运行它,代码在设备上构建并运行,但从未安排过作业, 没有收到消息。我尝试了一切,但仍然没有调用 jobService 类。我尝试在模拟器上再次运行该应用程序,它运行良好,但它从未在我的实时设备上运行。请问这可能是什么原因。我将 firebase gradle 依赖项添加到 build.gradle 文件中。我将 jobService 添加到清单文件夹中。我不知道造成这种困境的原因是什么。请我需要一个解释。谢谢。
主要活动
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();
}
}
}
工作服务类
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>
构建.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'
}