You need to get a service to talk to reality.

I need to create an action that starts a service that monitors the user's position, and when in a certain zone it allows the caller to update their view, informing the user.

Since there are several different ways to work with services, I am a little confused which one is right for my situation.

Use methods startService()andstopService() . From what I understand, I can’t directly return to the starting activity. There is an example in google docs showing how to pass BroadcastReceiver to a service via PendingIntent and call it, but I don’t think let me update the view of the current activity ... or will it?

Linked service . From the docs, it seems this will allow for a two-way connection between the service and activity, but it was also mentioned that related services are not running in the background indefinitely . Now I don’t need or even don’t want the service to work indefinitely, but in the worst case I might need it to run in bg for at least an hour or two without being killed.

The third (maybe not so big) option . Run the location service in the stream in an activity that will facilitate real-time updating, and then in the onPause event, stop the location service in action and transfer it to the service through startService()and use the notification service to alert the user when a certain zone is entered.

Any advice is appreciated.

  • it says

    if you want the service to send the result back, the client that starts the service can create a PendingIntent for broadcast (using getBroadcast ()) and deliver it to the service in the intent that starts the service. The service can then use the broadcast to deliver the result.

  • and it says

    A linked service usually lives only when it serves another component of the application and does not work in the background indefinitely.

+3
source share
3 answers

And, option (2), probably you want. You can override onStartCommandto return START_STICKY, and the Service will continue to work after the operation is completed.

bindService, , .

+5

, startService(). ServiceConnection, startService()/stopService(), .

android : Lifecycle

, , . ​​ , :

  • .
  • stopService().

, , .

+8

.

Put this code in your onCreate activity and define your service using the onBind method, which returns the middleware object. Define your IBinder correctly in your service.

This ensures that the service runs smoothly in the background. And every time you start your activity, he contacts the service. In the onDestroy () method of your activity, disable the service. Otherwise, it will throw a ServiceLeakedException.

   YourService service;   

   ServiceConnection  serviceConnection = new ServiceConnection() {
      public void onServiceConnected(ComponentName name, IBinder binderService) {
          service = ((YourService.ServiceBinder)binderService).getService();
      }

      public void onServiceDisconnected(ComponentName name) {

      }
   };

    // ... and bind.
    bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

Based on any calculation, your service can start your business. As it launches, it will contact the service. Then further communication may occur.

Hope this helps :)

+1
source

All Articles