0

I have a service that is working in the background - SERVICE A. When I open an activity - ACTIVITY A, I want this service to change the text that is displayed on the editText field of this ACTIVITY A. I thought that maybe if I get the context of the opened activity then I will be able to use and work on its Views. Is it possible?

Also a related question: If I don't have the name of the field, can I use some loop to run and search for all the views in the activity and get its properties?

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
HakoStan
  • 69
  • 2
  • 10

2 Answers2

1

It seems like a less direct method would be preferred to getting this, rather than have the activity be directly updated by the service. In fact, in general Service/Activity communications are best handled in one of the following ways:

  1. MessageHandlers- Basically, one can send a message to the other, which in turn the other sets the actions of the text.
  2. BroadcastReceivers (LocalBroadcastReceiver is also okay)- One part sends a broadcast message to anyone listening, the other sets up a listener for that message. An Intent is passed, which can contain the message.

I would suggest that you use the second. The application sends a broadcast to the service upon opening, and the service returns a broadcast to pass the requested data to the Activity.

See also the Service docs from the Android SDK.

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
  • is there anything to do if the activity is from a third party? I mean that I am not the developer of it. – HakoStan Aug 30 '15 at 19:54
  • 1
    That depends on the third party's API. In that case, you have to communicate with it using only public items. I suspect the feedback will be via a Broadcast Receiver in such case, but without seeing the API... – PearsonArtPhoto Aug 30 '15 at 20:06
0

1 save the string in preferences from service (see this : https://stackoverflow.com/a/12074248/4647577 )

2 retrieve the string when activity is created and set it to edittext

In your question you said that you want to show the string when you open the activity , so the 2 steps above will do the job .

For better performance you can repeat the second step in onresume();

Community
  • 1
  • 1
Mohamed Allam
  • 125
  • 2
  • 11