我刚刚为送货员创建了一个Android应用,管理员可以通过服务器为他分配职责。
问题是我想要一种功能,使应用程序可以每隔5分钟自动将纬度和经度发送到服务器,我曾经使用过Job Scheduler,但是它只能在我的旧手机(Android Lollipop)中使用,在Android Nougat中无法使用。
package com.example.dell.firebase;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
/**
* Created by DELL on 3/20/2019.
*/
public class MyService extends JobService{
BackgroundTask ba;
@Override
public boolean onStartJob(final JobParameters job) {
ba=new BackgroundTask(){
@Override
protected void onPostExecute(String s) {
Toast.makeText(getApplicationContext(),"hello all "+s,Toast.LENGTH_SHORT).show();
Log.d("dd","hg");
jobFinished(job,false);
}
};
ba.execute();
return true;
}
@Override
public boolean onStopJob(JobParameters job) {
return true;
}
public static class BackgroundTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... voids) {
return "hello from background";
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.dell.firebase.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:onClick="startjob"
android:text="start" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:onClick="stopjob"
android:text="stop" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
package com.example.dell.firebase;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static final String jobtag="Mytag";
private FirebaseJobDispatcher jobDispatcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jobDispatcher=new FirebaseJobDispatcher(new GooglePlayDriver(this));
}
public void startjob(View view) {
Job job=jobDispatcher.newJobBuilder()
.setService(MyService.class)
.setLifetime(Lifetime.FOREVER)
.setRecurring(true)
.setTag(jobtag)
.setTrigger(Trigger.executionWindow(10,15))
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setReplaceCurrent(false)
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
jobDispatcher.mustSchedule(job);
Toast.makeText(this,"job Scheduled",Toast.LENGTH_SHORT).show();
}
public void stopjob(View view) {
jobDispatcher.cancelAll();
Toast.makeText(this,"job Canceled",Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
我不确定,但是牛轧糖和之后的每5分钟都不能接受。.Job Scheduler的最小间隔时间是 15分钟,所以这可能是问题所在。帮助更多。