Continuous location updates in background

后端 未结 2 1775
一向
一向 2021-01-06 06:23

i am developing a application which will send location updates continuously from a background service. i tried following code.

public class LocationService e         


        
2条回答
  •  南方客
    南方客 (楼主)
    2021-01-06 06:54

    Try this:

    @Override
     public void onCreate() {
     //creating log file in mobile
        appendLog(DateFormat.getDateTimeInstance().format(new Date()) + ": Service created:", com.example.locationservice.Constants.LOG_FILE);
    
      mLocationClient = new LocationClient(getApplicationContext(), this,this);
     }
    

    replace your onStart with:

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    mLocationClient.connect();
    }
    

    and:

    @Override
    public void onConnected(Bundle arg0) {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setInterval(5*1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationClient.requestLocationUpdates(mLocationRequest, this);
    }
    

    Invoke your service:

    startService(yourServiceIntent);
    

    You can also check my code here

提交回复
热议问题