Why we use handlers with Thread :
When we install an application in android then it create a thread for that application called MAIN UI Thread, All activities run inside that thread , By the android single thread model rule we can not access UI elements (bitmap , textview etc..) directly for another thread defined inside that activity.
So if want to access Main UI Thread elements by another thread then we will use handlers.
So if want to access Main UI Thread elements by another thread then we will use handlers.
Thread With Handlers Example :
In this example we are creating a thread and call webservice to get server response and after got the response,then do other functionality ( Save Data in database or show alert ,Redirect to another activity).
AndroidManifest.xml file
Add Internet Permission
< uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > |
android_example_thread_handler.xml File
Create button with id : GetServerData
< Button android:id = "@+id/GetServerData" android:text = "Click To Get Server Data" android:cursorVisible = "true" android:clickable = "true" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center_horizontal" /> |
ThreadHandlerAndroidExample.java File :
public class ThreadHandlerAndroidExample extends Activity { @Override
|