-1

im an amateur programmer and i need help, i want to make update location when i click my switch button and get the location send the address to the retrofit every 1 or 5 minutes. Most likely im gonna use the 1 minute first to test it and make textview to test it.

i already searching and copy-paste, testing many codes from the internet. But im confused, many of them either won't work when the screen off, expired, in kotlin or just sending notification once. Even one of them didn't get me the address to test it for textview in Main Activity. I also heard about the Android phone that won't allow your app to run in the background.

Then i heard about Work Manager from here: https://medium.com/@Lakshya_Punhani/background-services-running-forever-in-android-100261df88ee

But idk how to do it. Can anyone show the the simple but complete codes to do it, so i can call it from my Main Activity and will still running in the background unless i press again my switch button from main activity?

Dioo B
  • 82
  • 9
  • It is recommended to go with Websockets . Open a socket session with your API and updated constantly in background. – Shogun Nassar Oct 19 '21 at 07:12
  • @ShogunNassar thanks, but sorry my brain can't take theory too much, i always learn from practice. i want to make it with work manager or something else if you please. i also want to try to send it with retrofit cuz my whole app using Retrofit and i been using it for a while now. – Dioo B Oct 19 '21 at 07:16
  • Check this example: https://robertohuertas.com/2019/06/29/android_foreground_services/ – Shogun Nassar Oct 19 '21 at 07:27
  • @ShogunNassar sorry, that example is in Kotlin, im using java – Dioo B Oct 19 '21 at 07:32
  • This is a helpful thread https://stackoverflow.com/questions/50761374/how-does-workmanager-schedule-get-requests-to-rest-api – Shogun Nassar Oct 19 '21 at 07:49
  • You have three main challenges here: 1) Schedule a task every 1 or 5 minutes. You could [Schedule tasks with WorkManager](https://developer.android.com/topic/libraries/architecture/workmanager) 2) You need to [Request location updates](https://developer.android.com/training/location/request-updates) in the scheduled task and remove the request once you get the location. 3) Send the data with e.g. Retrofit or take a look at [Volley overview](https://developer.android.com/training/volley) in Android documentation. – Markus Kauppinen Oct 19 '21 at 07:49
  • And a [Foreground Service](https://developer.android.com/guide/components/foreground-services) can be used for long lasting background operations. Otherwise it may be difficult to keep the Service running and receive location updates. (An `Activity` won't run in the background, so it has to be a `Service` anyway. A `ForegroundService` then is a special type of a `Service`.) – Markus Kauppinen Oct 19 '21 at 07:52
  • @MarkusKauppinen is there no other simple way to do it? like activated it from main activity with button, then getting location in work manager, send it back to main activity and set retrofit in main activity set retrofit to send the same variable every 1 minute with okhttp? – Dioo B Oct 19 '21 at 07:55
  • I'm not actually sure about the WorkManager. Maybe it doesn't need a Service. But it's all in the Android documentation. – Markus Kauppinen Oct 19 '21 at 10:54
  • But then there are some limitations in getting the location in the background. You can check [Access location in the background](https://developer.android.com/training/location/background) for details. – Markus Kauppinen Oct 19 '21 at 10:56
  • Anyway _"Can anyone show the the simple but complete codes to do it"_ is too broad for Stack Overflow, so you should try to make the individual parts work, ask more detailed question in case of trouble and then put it all together to get the full app. – Markus Kauppinen Oct 19 '21 at 10:57
  • @MarkusKauppinen okay, so i need to get how to make the class that run in the background first, and then how to get location in that background class every 5 or 1 minute, and then to send it to main activity ever 5 or 1 minute, and finally to make retrofit sending location every that period even in the background. GOT IT! – Dioo B Oct 21 '21 at 01:41

1 Answers1

-1

Handler to get location & address after every 1 or 5 seconds. Inside handler get location using FusedLocationProviderClient.

    var currentLatitude : Double = 0.0
    var currentLongitude : Double = 0.0
    private FusedLocationProviderClient fusedLocationClient;
        
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
          
        
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
        
    Handler(Looper.getMainLooper()).postDelayed({
            
    if (location != null) {
    
    currentLatitude = location?.latitude!!
    currentLongitude = location.longitude
    
    }
      }, 1000) //1000 milliseconds =  1 second
        
       }

Also don't forget to add permissions in manifest.

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Abdul Waheed
  • 562
  • 6
  • 14
  • im sorry man, im using Java, and is it gonna running in the background? am i not suppose to make new class for it? – Dioo B Oct 19 '21 at 07:27