0

I'm trying to develop an Android application consists of an Activity and a Service. The Activity launch a process on the Service of indefinite duration, which will be closed from Activity. Do not use then the subclass IntentService, but directly Service. Controlled by onStartCommand and OnDestroy.

I obviously need to pass information from the Activity to the Service: the status of the Service and some strings.

I tried to use LocalBrodcastManager, but when turning the devices or when the activity goes in state onPause, the message will lost. I tried to follow several examples, but with little success. This in particular I could not complete it because of some missing information, evidently deemed obvious, but which are not obvious to me: https://developer.android.com/training/run-background-service/report-status.html

I then tried to use Messenger via IBinder ( Example: Communication between Activity and Service using Messaging ), But the program seems a bit complex and I can not able to fit my needs.

What I need is to launch the service from my activity (possibly make binding automatically?, in case of Messenger use), the Service should signal the Activity to be active, then Service records some points via GPS LocationListener, writes it to a file and should point out, again the Activity, the data that is recording, the file size, etc.

What do you recommend to use to pass this information and can you provide to me some example?

Community
  • 1
  • 1
Paolovip
  • 123
  • 2
  • 10
  • You can configure your app so that a display orientation change does not kill and restart the Activity. This might be the easiest way to solve your problem. – David Wasser Apr 07 '15 at 14:41
  • Interesting, but what I must use? I was thinking it was not possible, and for this it was mandatory to use savedInstanceState... – Paolovip Apr 08 '15 at 06:28
  • Just declare in the manifest that your `Activity` will handle the orientation change itself. Override `onConfigurationChanged()` and when that is called you can reset the content view and/or change your layout. – David Wasser Apr 08 '15 at 13:15
  • Ah yes!, I see... android:configChanges="orientation|screenSize" Thank you, go to try. :-) – Paolovip Apr 09 '15 at 06:42

1 Answers1

1

I am actually in the midst of a tutorial explaining and comparing many different approaches to IPC in Android but since it's not ready and because you need an easy fix i'll recommend https://github.com/greenrobot/EventBus.

Also feel free to look in an old but still relevant example me and my friends made a while back here: https://github.com/RanNachmany/AndconLab

Goodluck.

Royi Benyossef
  • 769
  • 5
  • 18
  • Thank you very much. Your code is clear and very well written, but too much advanced for my actuals capabilities. For the moment I realized only half side of full process. I can only bind to the Service from my Activity, start ServiceConnection and getting a message into the Service. Now I must implement a Messenger in the client too, I suppose. But for the moment I must study how Handler really work. :-) – Paolovip Apr 07 '15 at 20:24
  • I would, but I do not know how. How is it done? (I am ashamed a bit ...) – Paolovip Apr 13 '15 at 08:02