Handler Does Not Execute Run - android

This feels like it should be a very simple task but I am having a lot of problems with it. In my program I have extended the WebView class for my own and am trying to add it to a layout programatically. Here is my code:
Looper.prepare();
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
MyWebView webView = new MyWebView(context,1,2,3,4);
appState.projectWebView.add(webView);
addView(webView);
}
});
When I run this code it does not execute. I have no idea why. Thanks for you time.

You cannot create a Handler outside of an UI thread. Well, you can, but you will have to turn that thread into a message queue with much more than just Looper.prepare().
What you need to do is pass an Activity to the class that contains the code in your sample, and call runOnUiThread() on it. Alternatively, you can pass a Handler created on a UI thread, for instance create it at the thread that runs your Activity UI, and then call post on that handler.
Note that this is awful advice, you seem to be trying to do things against the Android framework. But, without further information of what you are actually trying to do, there is no much that can be said.

Related

Execute a function without blocking the application android?

How to call and execute a function without blocking the application android (this is the location function).
I know I must use the thread but I do not know how, I hope to have a code ready.
Use the following to run code on a separate (non-UI) Thread:
new Thread(new Runnable(){
public void run(){
//do stuff here
}
}).start();
This creates a new Thread (and corresponding Runnable which contains the code to be run inside its run() method ) and starts it -- calling the code in the Runnable.
Alternatively, you could look into AsyncTask (more info here) which avoids using Threads directly.

Handler or runOnUiThread solution for "Can't create handler inside thread that has not called Looper.prepare() "

Recently, I show up a toast in user thread and got the above runtime error.
From Can't create handler inside thread that has not called Looper.prepare(), they proposed use Handler as solution. However, I saw the solution is quite lengthy and cumbersome.
My own solution is to use runOnUiThread
private void showTooDarkToastMessage()
{
((Activity) this.getContext()).runOnUiThread(new Runnable() {
#Override
public void run() {
Toast toast = Toast.makeText(getContext(), getResources().getString(R.string.toast_toodark), Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
});
}
I was wondering, is there any shortcoming of using runOnUiThread, compared to Handler?
Because you are showing a UI element (a Toast message) runOnUiThread is perfect.
A Handler will run its task on the specified thread. For instance,
protected void onCreate( Bundle savedInstanceState )
{
Handler hander = new Handler();
//Create thread, post to handler
}
would create a new Handler that would run its posts on the UI thread. calling Activiy.runOnUiThread just posts the runnable specifically to the UI thread. By default, Handlers will run on whatever thread they were created in. The above code would work identical to using runOnUiThread because the onCreate method is run on the UI thread!
Handlers would be preferred if you needed to communicate between multiple background threads.
Because mobile devices have limited resources, work run on the UI thread should be kept relatively light. Intense work done on the UI thread can cause Application Not Responding (ANR) errors and can cause the OS to kill your process.
Actually runOnUiThread() using Handler inside. So, there is no downsides to use runOnUiThread() if you want to simple post some job to do in the UI Thread.
If you are interesting in difference between Handler and runOnUiThread() you can read about it in this answer

A WebView in a thread can't be created

i have some threads in which i create some views and prepare them to be displayed. Among them i also have a WebView. This code is executed in thread:
WebView lGraphWebView = null;
try{
lGraphWebView = new WebView(AppController.getAppController());
}catch (Exception e) {
Log.d("info", "error: " +e );
}
and it throws the following exception:
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
This is a bit strange, because when i create a simple button all is OK. So, can anyone explane to me why on creation of a WebView i get this exception and if Looper.prepare() can help here?
Thanks in advance!
In general, its not safe to create view outside of main thread.
In your particular case, this is not allowed, because WebView creates Handler() in its constructor for communication with UI thread. But since Handler's default constructor attaches itself to current thread, and current thread does not have Looper running, you're getting this exception.
You might think that creating a looper thread (that must be alive at least as long as WebView) might help you, but this actually a risky way to go. And I wouldn't recommend it.
You should stick with creating WebViews in main thread. All controls are usually optimized for fast construction, as they are almost always created in UI thread.
You should not create or manipulate views in threads other than the main UI thread. For instance, you can use the Handler to post to the UI thread:
private Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
lGraphWebView = new WebView(AppController.getAppController());
}
});

Runnable run() method called twice sometimes?

I am a new Android developer. I am using the Handler class to schedule some operations. So I am creating runnable objects that calls some of my instance methods.
But I have a problem. Sometimes my run() method in Runnable object is called twice.
What could be the problem??
and there is the code
//deneme is a Handler.
deneme.postDelayed(new Runnable() {
#Override
public void run()
{
randomOyna();
//the instance method that I call.
}
}, 1000);
If don't schedule your Handler to run on another Thread than the UI-thread, there might be a hidden delay in the execution because your Runnable will also run on the UI-thread and thus will only be allowed to run when there is "time" for it. With this hidden delay, it might seem like it is run twice but in reality it's just and over-delayed running before a regular delayed Runnable.
Can't see a mistake just by looking the hint you gave us... But you might try plain old java to run threads instead of handler... Good luck...
Look here for more details

Handlers in Android Programming

I have got to know that the Handlers are basically used to Run the Small Section of Code and etc...
But I didn't got the exact scenerio about when particularly It is Ideal to use the Handlers!
Any Help???
Thanks,
david
Handlers are used for updating the UI from other (non-UI) threads.
For example, you can declare a Handler on your Activity class:
Handler h = new Handler();
Then you have some other tasks on different thread that wants to update some UI (progress bar, status message, etc). This will crash:
progressBar.setProgress(50);
Instead, call this:
h.post(new Runnable() {
public void run() {
progressBar.setProgress(50);
}
});
I'm a newbie myself but I'll give a newbie example since I recently learned this, I'm sure there are many more.
You have to use a Handler when you want to update the main UI when you are doing something in another thread. For example in my case I used it in image slideshow code that runs in a TimerTask. You cannot update the main UI ImageView with the next image from within the TimerTask because it's in a different thread. So you have to use a Handler or you get an error.
This is just one example. I hope this helps.

Categories

Resources