Running a service to get location details

前端 未结 3 1822
日久生厌
日久生厌 2021-02-11 02:25

In my app, i need to create an android service to periodically get mobile user\'s current location(latitude and longitude). The service should get user\'s location in every 5 mi

3条回答
  •  旧时难觅i
    2021-02-11 03:14

    You don't need a service to accomplish this. In order to get location every 5mins, in your app, on the activity you can implement the location listener. While setting up the location manager, you can specify the minTime = 5mins (300000 ms). The onlocationChanged method will be called every 5 mins or when the distance change > minDistance, which ever occurs first.

    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    MyLocationListener listener = new MyLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, minDistance, listener);
    

提交回复
热议问题