Get the current device location after a certain interval

I want to capture the current location (latitude and longitude) of an Android device after a certain interval (say, 30 minutes). My class (or the service is not sure what to use) will start listening to the LocationManagerListener when the device boots up. What is the best way to implement this? how can we use the locationChanged() method in this script?

Here is what I think he can go:

Listen to the downloaded completed event and install the alert service:

 public class OnBootReceiver extends BroadcastReceiver { private static final int PERIOD=1800000; // 30 minutes @Override public void onReceive(Context context, Intent intent) { AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i=new Intent(context, OnAlarmReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+60000, PERIOD, pi); } } 

Listen to the alarm service and initiate a class or location capture service:

  public class OnAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { WakefulIntentService.acquireStaticLock(context); context.startService(new Intent(context, locationCapture.class)); or new locationCapture().classmethod(); } } 

I am not sure how the locationCapture class should be implemented. Should there be a regular Java class or a service class?

Any help would be appreciated.

+4
source share
2 answers

This class of service you can use

 public class ServiceLocation extends Service{ private LocationManager locMan; private Boolean locationChanged; private Handler handler = new Handler(); public static Location curLocation; public static boolean isService = true; LocationListener gpsListener = new LocationListener() { public void onLocationChanged(Location location) { if (curLocation == null) { curLocation = location; locationChanged = true; }else if (curLocation.getLatitude() == location.getLatitude() && curLocation.getLongitude() == location.getLongitude()){ locationChanged = false; return; }else locationChanged = true; curLocation = location; if (locationChanged) locMan.removeUpdates(gpsListener); } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider, int status,Bundle extras) { if (status == 0)// UnAvailable { } else if (status == 1)// Trying to Connect { } else if (status == 2) {// Available } } }; @Override public void onCreate() { super.onCreate(); curLocation = getBestLocation(); if (curLocation == null) Toast.makeText(getBaseContext(),"Unable to get your location", Toast.LENGTH_SHORT).show(); else{ //Toast.makeText(getBaseContext(), curLocation.toString(), Toast.LENGTH_LONG).show(); } isService = true; } final String TAG="LocationService"; @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onLowMemory() { super.onLowMemory(); } @Override public void onStart(Intent i, int startId){ handler.postDelayed(GpsFinder,1); } @Override public void onDestroy() { handler.removeCallbacks(GpsFinder); handler = null; Toast.makeText(this, "Stop services", Toast.LENGTH_SHORT).show(); isService = false; } public IBinder onBind(Intent arg0) { return null; } public Runnable GpsFinder = new Runnable(){ public void run(){ Location tempLoc = getBestLocation(); if(tempLoc!=null) curLocation = tempLoc; tempLoc = null; handler.postDelayed(GpsFinder,1000);// register again to start after 1 seconds... } }; private Location getBestLocation() { Location gpslocation = null; Location networkLocation = null; if(locMan==null){ locMan = (LocationManager) getApplicationContext() .getSystemService(Context.LOCATION_SERVICE); } try { if(locMan.isProviderEnabled(LocationManager.GPS_PROVIDER)){ locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000, 1, gpsListener);// here you can set the 2nd argument time interval also that after how much time it will get the gps location gpslocation = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); } if(locMan.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000, 1, gpsListener); networkLocation = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); } } catch (IllegalArgumentException e) { //Log.e(ErrorCode.ILLEGALARGUMENTERROR, e.toString()); Log.e("error", e.toString()); } if(gpslocation==null && networkLocation==null) return null; if(gpslocation!=null && networkLocation!=null){ if(gpslocation.getTime() < networkLocation.getTime()){ gpslocation = null; return networkLocation; }else{ networkLocation = null; return gpslocation; } } if (gpslocation == null) { return networkLocation; } if (networkLocation == null) { return gpslocation; } return null; } } 

You can set the time in the handler or in requestLocationUpdates() . you need to run this service from home. As in I, I set 1 sec for the handler and requestLocationUpdate() to get the location after 1 second and updated me.

Edited: Since this service to get the current location of the user, you can start with home activity, as well as start with the download time. If the home activity was sure that if the service was stopped by the user using another application, for example, a task killer, then when the user starts your application and then from home this service will be started again to start the service, you can do it as follows.

 startService(new Intent(YourActivity.this,ServiceLocation.class)); 

When you need to stop servicing, it will call onDestroy () so that the handler can cancel the thread in order to continue receiving it.

Since GPS is set with 1 second (1000), this will get the gps location every 1 second, but to get this place you need to call every time, and in my case I set 1 second and according to your requirement up to 30 s. Thus, you get the location every 1 second and using this service in the handler, set 30 minutes. To save battery life, you can also set the time in GPS mode to save battery life.

you can delete the comparative part of the location and the current location, because the locationlistener is called every time the location has been changed, and you can use curLocation anywhere in the application to get the current location, but first make sure that you need to start this service first after as you can use otherwise you will get a null pointer exception when accessing the latitude and longitude of this object

I hope you have an idea, and I received an answer about your requests

+6
source

This source code guide for the various API levels comes from the source:

http://android-developers.blogspot.com/search/label/Location

0
source

Source: https://habr.com/ru/post/1384258/


All Articles