views:

44

answers:

2

Hi,

can someone please help me?

I would like to write a program which uses a service to periodically update a text view on an activity.

I do this by having ActivityA with a 2 buttons to start/stop my service. In the service I run a timer which triggers every second. From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.

I'm sure there are likely better ways to do this, such as using only one activity, maybe using a thread but the main design consideration is to have the service running even if my activity is destoyed (the counter value would instead go trigger some alarm or file write instead of a text view update).

Sorry for rambling. I find the android developer resources offer too many solutions!

Thanks

Ben

A: 

In the service I run a timer which triggers every second.

Why? Most Android devices run on batteries. Batteries are never big enough. What value are you giving the user to justify your expenditure of CPU and RAM (and, hence, battery life)?

From here I need to have this launch and update a text view on ActivityB which at present is just a counter value.

Where is "here"?

I'm sure there are likely better ways to do this, such as using only one activity

I would think so.

maybe using a thread

Probably not.

but the main design consideration is to have the service running even if my activity is destoyed

This is significantly more complicated than you are perhaps thinking.

(the counter value would instead go trigger some alarm or file write instead of a text view update).

If your goal is to do something at a particular time, use AlarmManager.

I suspect that there is a better approach for whatever it is that you are trying to do than the path you are presently headed down. Unfortunately, since I do not know what it is that you are trying to do, I have limited ability to provide more specific advice.

CommonsWare
Ok, I was trying to keep it vague as I'm still learning the concepts, but I guess that wasn't helpful.The description is all just a way for me to test the basic structure before I code functionality. The eventual intended functionality is a engineering field menu type app, where a telephony state change is detected by a service such that the service updates an activity or widget. I know I could do this in a single activity but I want the telephony detection to run when the activity is not in focus (or whatever the term is for top of the application stack)
Ben Ash
@Ben Ash: Some forms of "telephony state change" are issued via broadcast `Intents`. For those, you register a `BroadcastReceiver`, either from your activity via `registerReceiver()` or in your manifest. You would go with the former if you only care about the events while the activity is running; the latter would be if you needed to log the events to a database or something. So, for example, you would use this technique for `ACTION_PHONE_STATE_CHANGED` and the like.
CommonsWare
A: 

I think what you want to do is at best done with an simple AsyncTask. If you use the onProgressUpdate method you can increase the value in the textview at every time you reach a certain point during your background work. It is also able to cancel the background work etc. There is no need for the full Service, Thread work.

Janusz