Threads
Every application by default runs a single process upon creation that contains all the tasks.To avoid hanging the user interface, time-consuming tasks, such as network downloads orcomputationally intensive calculations, should reside in a separate background thread. It is up to the developer to implement this properly, but then the Android operating system (OS) prioritizes the threads accordingly.
Most applications can benefit from the use of threads.
Every application by default runs a single process upon creation that contains all the tasks.To avoid hanging the user interface, time-consuming tasks, such as network downloads orcomputationally intensive calculations, should reside in a separate background thread. It is up to the developer to implement this properly, but then the Android operating system (OS) prioritizes the threads accordingly.
Most applications can benefit from the use of threads.
ThreadExampleActivity.java
package
Android.code.thread;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
public class
ThreadExampleActivity extends Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread t = new Thread(new Runnable()
{
public void run(){
try{
Thread.sleep(5000);
}
catch(InterruptedException
e){
}
// To do
startActivity(new Intent(
"Android.code.thread.activity2"));
}
});
t.start();
/*
if(t != null){
Thread dummy = t;
t = null;
dummy.interrupt();
}
*/
}
}
main.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#55D5F5"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Thread
Example"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000"
android:textSize="30dp"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#AA66CC"
android:text="Thread
Running,
Wait for 5
sec."
android:textColor="#000000"
android:textSize="24dp"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="115dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_weight="0.02"
android:src="@drawable/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="240dp"
android:text="main.xml"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0000FF"
android:textSize="26dp"
/>
</LinearLayout>
Activity2.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF4444"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#FFBB33"
android:text="After 5 sec,
Display this activity."
android:textColor="#000000"
android:textSize="30dp"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:src="@drawable/ic_launcher"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="200dp"
android:text="activity2.xml"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com