等级
我是初学者
任务
运行Android服务,每隔5分钟在驱动程序驱动(跟踪位置)时定期向数据库发送位置更新(纬度,经度)。我想用位置更新数据库,即使应用程序不活动,因此我决定使用firebase作业调度程序。
注意
我已经看过几个关于如何使用ALARM_MANAGER或广播接收器等执行此操作的线程。我想知道如何使用作业调度程序执行此操作。我当前的代码可以,但我很确定它不是正确的方法。
问题
1.我在哪里打电话来构建Googleapi并调用Connect()方法?现在,我的调度程序调用了一个类'onStart',它继而执行连接。
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清单中注册服务并在build gradle中注册依赖。