Android - Interact with the main thread every few milliseconds - android

I'm new to Android, so apologies if I'm missing anything obvious.
I'm writing an app that does something every few hundred milliseconds. The frequency varies, but no more often than every 300 or so.
Currently I have a class DoEvery that implements Runnable that is scheduled every X milliseconds using scheduleAtFixedRate from the main thread. That is working, but I want to add an animation that is started every time DoEvery.run executes using Drawable.start() and from what I've read it doesn't seem like that is possible since Drawable.start() needs to be run on the main thread. Using scheduleAtFixedRate also seems to make it difficult to change the frequency later.
Is there a way to start the animation from the DoEvery class? Or is there a better way to run something on a regular basis like this?

You could try this.
new Thread(new Runnable() {
#Override public void run() {
//I'm in the thread.
//if you are not in the Activity, pass the activity instance to your class
// and use myActivity.runOnUiThread(...)
runOnUiThread(new Runnable() {
#Override public void run() {
//I'm in the main thread
}
});
}
}).start();

Related

Android multiple posts with delay

I have a set of instructions that needs to be executed repeatedly.
The repeat delay is variable (the delay is taken from a file).
The repeat time must be very precise (milliseconds precision).
I have tried to use a Handler with postDelayed but the accuracy is lost in time. This happens because the repeat frequency is more than 10 times/second.
Any idea is more than welcomed!
Here is the Handler example:
new Runnable() {
#Override
public void run() {
mHandler.postDelayed(this, delay);
barometerResult.gotBarometer(barometerValueModelList.get(i).getBarometerValue());
}
}.run();
I don't think it's a good idea using Handler, it runs in the UI thread with many other things to do in the queue, so that's not guarantee your runnable code would be executed so precisely.
If your work in the Runnable is just some logic task not involved in update UI.Consider using a Timer to do that but be ware that it will run on a separated thread.

Advanced MultiThreading in android

I'm new to android, I need to start a Thread Multiple times to do a regular work as the listen a thing. But I searched and found that can't do this. So I decided to put
while(true){
listen some thing
do another something depends for listen
}
in the run() method.
But How can I implements this idea? Is it possible ? and How?
You should use Handler.post() whenever you want to do operations in the UI thread.
So let's say in the callback (which is running in separate thread) you want to change a TextView's text, you should use Handler.post(). In Android, as in many other UI frameworks, UI elements (widgets) can be only modified from main thread.
mHandler = new Handler();
new Thread(new Runnable(
#Override
public void run () {
// Perform long-running task here
// (like audio buffering).
// you may want to update some progress
// bar every second, so use handler:
mHandler.post(new Runnable() {
#Override
public void run () {
// make operation on UI - on example
// on progress bar.
}
});
}
)).start();
Of course, if the task you want to perform is really long and there is a risk user might switch to some another app in the meantime, you should consider using Service.
If you start your thread inside a while loop with true condition, it'll kill your device. You'r device will hang. Your code should be event triggered. You can have a Service running in the background to do that and listen for your events.

How to control time activated display update

I am looking for the method to update the screen at a constant rate, say every 50mSec.
In the embedded world I would configure a timer to trigger an interrupt every 50mSec and the ISR would handle the update. What I have in mind for Android is to have a FrameLayout with a number of view items registered to react to some sort of callback from a timer function.
Hopefully someone will understand my question and point me in the right direction.
You may use handler.postDelayed in a Runnable to call it after certain time period.
For example:
handler.post(new Runnable() {
#Override
public void run() {
handler.postDelayed(this, 50);
//do your task
}
});
Make sure you handler is attached to a separate (non-UI) thread and when it comes to update UI elements you always do it on a UI thread.

android: what's the difference between a timertask and a service?

I'm using a timertask in the main activity in order to update some data repeatedly every X seconds.
This data is of static form, so it's a public static method
is this a bad technique? I mean, using static methods like this
I know there's this thing called Service, but there aren't really many examples online on how to use it in order to update every X seconds a variable that should be then accessed by some activity
so my question is, what's the difference between using a timer task and a service? is a timertask just a time counter and nothing else? does it run in parallel if it's being used with a handler or not? and what happens if you have something like this:
handler = new Handler();
t = new Timer();
task = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
//do stuff;
}
});
}
};
t.schedule(task, 0, 10000);
how exactly does this code work? what does it do? does it run in parallel? why even if I leave the activity where this code is first being called, it still runs? what makes it run in this case?
thanks in advance
TimerTask is part of standard Java and can be use for delayed or repeated execution for some piece of (Runnable) code. It's use is discouraged on Android. You can use a Handler instead.
A Service can be used as a independent and UI-less part of your Android application. It can run and create it's own threads and can be started for UI or with Intents through a AlarmManager for example.
It think want you want is a Service which creates it's own thread and does some work. When the work is done, memory will be freed on Android when the garbage collector kicks in, something you do not control and that's a good thing.

Programmatically determine whether the code is executing from the UI thread or not

In my Android app, I am extracting the code to update UI elements into a separate utility package for reuse. I would like my code to be proactive and update the UI differently if the current execution context is from a UI thread versus a non-UI thread.
Is it possible to programmatically determine whether the current execution is happening on the UI thread or not?
A trivial example of what I am looking to achieve is this - my app updates a lot of TextViews all the time. So, I would like to have a static utility like this:
public static void setTextOnTextView(TextView tv, CharSequence text){
tv.setText(text);
}
This obviously won't work if called from a non-UI thread. In that case I would like to force the client code to pass in a Handler as well, and post the UI operation to the handler.
Why don't you use the runOnUiThread method in Activity
It takes a runnable and either runs it straight away (if called from the UI thread), or will post it to the event queue of the UI thread.
That way you don't have to worry about if your method has been called from the UI thread or not.
When you're not sure the code is executed on the UI thread, you should do:
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
// your code here
}
});
This way, whether you're on the UI thread or not, it will be executed there.
You can use the View post method.
Your code would be:
tv.post(new Runnable() {
public void run() {
tv.setText(text);
}
});

Categories

Resources