Advice on structuring a service, activity, thread - android

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

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.

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.

Related

In Android it is better to use bounded service for communicating with a fragment or a service with a eventbus system (MessageBus) is enough?

Im building an Android App in Xamarin that plays music.
I have a service that plays the music and a fragment that display the playlist, some times i want a small feedback from the service and so far i was using MessageBus for the communication.
For now the only time that i want a feedback is when the user has select to loop the whole list and when the current track is finished i need to inform the fragment which is the next track in order to highlight it. maybe in the future i will need for feedback.
Is this a bad idea? i should better change the service with a boundService? I have select this method because it is much simpler and the feedback i want is very limited but now i have second thoughts.
Thanks
As long as you only need to communicate within your own application, there's nothing wrong with using a message bus.
I do this quite a bit, and it works well, and is much less complicated than other methods.
Just make sure that you know the threading model that your event bus is using, and be careful to do non-ui things on a background thread, and ui updates on the main ui thread.

App is slowing down [General]

I do have a more general question, without any specific code. I will explain what my application does and how and what issues I can monitor. Maybe one of you had the same issues and can lead me to the problem.
The App:
It reads car diagnostic data (OnBoardDiagnostics) over Bluetooth and shows them in real-time in a ListView. I can start the update function by a "update Button".
How:
Everytime a new value is received via Bluetooth, a background Class (which handles the Stringforming) sends an Intentto notify the UI to update the ListView.
The Adapter Class of my ListView has the listening BroadcastReceiver registered and if it gets triggered, it will notify the ListView by notifyDataSetChanged().
Issues:
1.If I use an WakeLock to keep the screen on, the UI refreshing slows down after approx. 10 minutes.
2.If I press the power button, so the screen is off, it still slows down (I can see that, because I send the values to an webserver) but furthermore: If I turn the screen back on. I see the ListView stops for about 20-30 seconds and than normally continues with normal speed (not slow anymore).
So.. I think this is a very general question. I searched for WakeLock and sleep behaviour, but I couldn't find any similar issues. Maybe one of you can give me a hint, what the problem could be. Maybe one of you had a similar problem.
Any hint is appreciated!
EDIT 1:
Maybe the problem of the 2. issue is based on the lifecycles of my objects / activity.
If I press the update Button, an AsyncTask is started, which sends the Data (JSON, which contains one new value for all list items) to my Webserver. If the device screen is off, I still get the data every 2 seconds. If I turn on the screen, it stops for these 20-30 seconds as well as the UI. So I think my UI works fine. The Update Intents were sent right.
I have to check if I still receive new values in that background class, mentioned above.
Thanks to zapl
Thanks!
Except all possibilities I checked, i came across this article:
AsyncTasks for long running Operations
Short: There are some points you need to keep in mind if you are using AsyncTasks in very long running operations (>20min). My Problem was, that I used the AsyncTask as an inner Class. After a long period, when the Activity that created the Task was destroyed, the AsyncTask still kept a reference of this activity.
After I used a Bus, described in the article above, the UI worked fine!!
So, if anyone else noticed performance problems of your App, I recommend that article.
Thanks for all the other hints!
Have fun coding!

android: run timer for long period

if i want to run a timer for a long time in android and show the user, when they go to a particular activity, for example, the duration since the timer started as a live number - how should I implement this? By live, i mean the time shown changes in real time as one would expect a timer to do. There will only need to be one instance of this timer, it'll pretty much be like android's stopwatch but implemented to function within a custom app.
Would I require wakelock?
should I create a service?
or should I just use a simple java timer?
any help/advice much appreciated.
thank you.
What do you mean 'long timer' 1 minut, day?
You have to realise that android can kill you activity any time, so the activity is not the right place to do it.
You can run you operation in Service - his life is longer then activity's, but you probably want to check time even if user returns in activity after week or reboot device.
If you tell more info about what you want it'll be easier to solve you problem.
show the user, when they go to a particular activity
As I see you problem the solution should be like this:
First enter to the activity - create timer, start it,
On activity stop save current value of the timer and system time,
On recreate activity read saved value and start new timer with value of init_value + (current_time - saved_time).
In this case you can be sure that timer is persistant even if user left your activity and even restart device.
And also battery life will be much more longer :)

Handler or Alarm Manager for writing to a file ever second

If I want to write to a file every 1 second some infomation that I get from the phone (in addition to updating activity GUI), then would I be better using a recursive handler(postDelayed) or should I use alarmmanager?
Which one is more stable? Which one would you recommend? My worry with the handler that if one timeout crashes (or for some reason didn't get called) then none of the rest will start. With Alarm manager, I don't think you have this but "some people" say it is too heavy for 1 sec intervals and you don't have much control over gui (I find it hard to belive the later statement).
Please advise
Thank you

How to know for how much long the application is running in android?

I am doing an application in android which requires to know for how long the application is running. Do anyone know how to retrieve such information??. Is there any way where android provides the information about the running applications ,from how much time they are running??
I'm not aware of any method which would handle that but you can simply implement it yourself. Just capture the current time in seconds and in the onPause() method do the same. Then just subtract the first saved time value and the last saved value and you should know for how many seconds the application was running.
There might be another more elegant solution I don't know of tho.
You could user SystemClock.uptimeMillis() or SystemClock.elapsedRealtime() in your main activity once it is first launched. Then each time you need to see how long the app is running call the method again and subtract from the original value.
There is some data collected for all applications to show battery usage by application, but I've not done the research to find out how to access it programmatically (if even possible) and am not sure when it is reset (perhaps on every charger/usb disconnect).
For one application the previous suggestions about instrumenting create and pause/resume methods sounds best.

Categories

Resources