级别
I 是初学者
任务
运行一个 android 服务,该服务在司机每 5 分钟驾驶(跟踪位置)时定期向数据库发送位置更新(纬度、经度)。即使应用程序未处于活动状态,我也想使用位置更新数据库,因此我决定使用 firebase 作业调度程序。
注意
我已经看到了几个关于如何使用 ALARM_MANAGER 或广播接收器等执行此操作的线程。我想知道如何使用作业调度程序执行此操作。我当前的代码有效,但我很确定它不是正确的方法。
问题
1. 我在哪里调用来构建 Googleapi 并调用 Connect() 方法?现在,我的调度程序调用一个类“onStart”,它依次执行连接。
- 除了使用 jobScheduler 之外,还有什么方法可以让我使用“位置接收器”来执行此操作,因为我读取它需要一个时间间隔,并通过覆盖返回更新的位置
onLocationChanged()
。
当前工作代码
Firebase 服务:-
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.firebase.jobdispatcher.JobParameters;
import com.firebase.jobdispatcher.JobService;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class GPSTracking extends JobService {
private AsyncTask mBackgroundTask;
public static final String LOG_TAG = "location-scheduler";
@Override
public boolean onStartJob(final JobParameters jobParameters) {
Log.i(LOG_TAG,"Scheduling");
mBackgroundTask = new AsyncTask() {
@Override
protected Object doInBackground(Object[] params) {
Context context = GPSTracking2.this;
LocationTracker.execute(context);
return null;
}
@Override
protected void onPostExecute(Object o) {
jobFinished(jobParameters, false);
}
};
mBackgroundTask.execute();
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
if (mBackgroundTask != null) {
mBackgroundTask.cancel(true);
}
return true;
}
}
实际跟踪:-
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
public class LocationTracker implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private GoogleApiClient mGoogleApiClient;
private Location mLastLocation;
private Context context;
private static final String LOG_TAG="LOCATION TRACKER";
public LocationTracker(Context context){
this.context = context;
buildGoogleApiClient();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
//Disconnect
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
public static void execute(Context context){
LocationTracker2 track = new LocationTracker2(context);
//connect
track.startConnection();
}
public void startConnection(){
mGoogleApiClient.connect();
}
private static void trackLocation(Location location) {
// To replace with updating database
if (location!=null) {
Log.i("TRACKING LAT", String.valueOf(location.getLatitude()));
Log.i("TRACKING LON", String.valueOf(location.getLongitude()));
}
else
{
Log.i("TRACKING LAT", "Null location received");
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
// Create new location request
// The permission should be granted previously
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
trackLocation(mLastLocation);
}
@Override
public void onConnectionSuspended(int i) {
Log.i(LOG_TAG,"Connection suspended");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(LOG_TAG,"Connection failed");
}
}
在 Main 中,启动调度:
Driver driver = new GooglePlayDriver(context);
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);
Job locationTrackerJob = dispatcher.newJobBuilder()
.setService(GPSTracking.class)
.setTag(JOB_TAG)
.setLifetime(Lifetime.FOREVER)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(
REMINDER_INTERVAL_SECONDS,
REMINDER_INTERVAL_SECONDS + SYNC_FLEXTIME_SECONDS
))
.setReplaceCurrent(true)
.build();
dispatcher.schedule(locationTrackerJob);
注意
- 您可能需要添加代码以根据 api 检查权限
- 我将忽略在 Android 清单中注册服务和在构建 gradle 中的依赖项。