Android Threading Example

I want a simple example of creating threads and calling threads in android.

+62
android multithreading
Jan 18 '11 at 10:28
source share
3 answers

One of the powerful features of Androids is the AsyncTask class.

To work with it, you must first expand it and override doInBackground (...). doInBackground automatically executed in the doInBackground , and you can add some listeners in the user interface thread to receive notification of the status update, these functions: onPreExecute() , onPostExecute() and onProgressUpdate()

You can find an example here.

Refer to the following post for other alternatives:

Handler vs AsyncTask vs Thread

+9
Jan 18 2018-11-11T00:
source share

Here is a simple thread example for Android. It is very simple, but it should help you get a perspective.

Android code - Main.java

 package test12.tt; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Test12Activity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView txt1 = (TextView) findViewById(R.id.sm); new Thread(new Runnable() { public void run(){ txt1.setText("Thread!!"); } }).start(); } } 

Xml android application - main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id = "@+id/sm" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> </LinearLayout> 
+5
Oct 10 '11 at 2:25
source share



All Articles