編輯:關於Android編程
最簡單的一個線程應用的例子:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainActivity);
new Thread(){
@Override
public void run(){
System.out.println("Thread is starting...");
}
}.start();
}
}
使用Runnable的例子:
Runnable runnable = new Runnable() {
@Override
public void run(){
System.out.println("Runnable is starting...");
}
};
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainActivity);
new Thread(runnable).start();
}
}
常見的Multi-Thread的用法:
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainActivity);
MyTestThread myTestThread1 = new MyTestThread();
MyTestThread myTestThread2= new MyTestThread();
MyTestThread myTestThread3 = new MyTestThread();
myTestThread1.start();
myTestThread2.start();
myTestThread3.start();
}
}
class MyTestThread extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is running...");
}
}
運行結果:
I/System.out(672): Thread-10 is running...
I/System.out(672): Thread-11 is running...
I/System.out(672): Thread-12 is running...
以上代碼還可以使用實例化Multi-Thread的用法:
MyTestThread myTestThread = new MyTestThread();
new Thread(myTestThread, "My Thread 1").start();
new Thread(myTestThread, "My Thread 2").start();
new Thread(myTestThread, "My Thread 3").start();
運行結果:
I/System.out(672): My Thread 1 is running...
I/System.out(672): My Thread 2 is running...
I/System.out(672): My Thread 3 is running...
線程休眠:sleep (待續)
線程優先級:priority (待續)
線程讓步(調度):yield (待續)
後台線程:daemon (待續)
使用執行器:Executer (待續)
定義任務:Runnable (待續)
最近項目裡有用到ViewPager來做廣告運營位展示,看到現在很多APP的廣告運營位都是無限循環的,所以就研究了一下這個功能的實現。先看看效果從一個方向上一直滑動,麼有滑
沒什麼技術難點,照著api做的。 這是效果圖 我們先對Toast自定義一個布局: 下面是關鍵代碼,我
1.目標 androidannotation框架要促進Android應用程序的編寫和維護。相信簡單的代碼有明確的意圖是實現這些目
UI的畫圖流程中,先不管怎麼填充要畫的數據的,只是來看一下需要畫到屏幕上的數據是通過怎樣的流程最終傳遞到屏幕上的。這個流程都是UI獲取並創建Surface並利用Cavan