您好,登錄后才能下訂單哦!
Handler的實質就是異步消息處理。
實例一:
Handler的基本用法
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tvResult" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="高興點,未來不是夢"/> <Button android:id="@+id/btnStart" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="動起來!"/> <Button android:id="@+id/btnEnd" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="歇會吧!"/> <Button android:id="@+id/btnClear" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="清零!"/> </LinearLayout>
Java代碼:
public class HandlerActivity extends Activity { private Button btnStart; private Button btnEnd; private Button btnClear; private TextView tvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handler); tvResult = (TextView) findViewById(R.id.tvResult); btnStart = (Button) findViewById(R.id.btnStart); btnEnd = (Button) findViewById(R.id.btnEnd); btnClear = (Button) findViewById(R.id.btnClear); btnStart.setOnClickListener(new StartButtonListener()); btnEnd.setOnClickListener(new EndButtonListener()); btnClear.setOnClickListener(new ClearButtonListener()); } // 創建一個Handler對象 Handler handler = new Handler(); private int i = 0; // 將要執行的操作寫在線程對象的run方法當中 Runnable updateThread = new Runnable() { public void run() { System.out.println("高興點,追夢!--" + i); tvResult.setText("高興點,追夢!--" + i); i++; // 在run方法內部,執行postDelayed或者是post方法 handler.postDelayed(updateThread, 3000); }; }; class StartButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調用Handler的post方法,將要執行的線程對象添加到隊列當中 handler.post(updateThread); } } class EndButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調用Handler的post方法,將要updateThread從隊列當中移除 handler.removeCallbacks(updateThread); } } class ClearButtonListener implements OnClickListener { @Override public void onClick(View v) { // 調用Handler的post方法,將要updateThread從隊列當中移除 handler.removeCallbacks(updateThread); i = 0; tvResult.setText("高興點,未來不是夢"); } } }
實例二:
Handler與ProgressBar的結合使用
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressbar" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" /> <Button android:id="@+id/btnStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="前進吧,小摩托!" /> </LinearLayout>
Java代碼:
public class HandlerProgressBarActivity extends Activity { private ProgressBar progressbar; private Button btnStart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlerprogressbar); progressbar = (ProgressBar) findViewById(R.id.progressbar); btnStart = (Button) findViewById(R.id.btnStart); btnStart.setOnClickListener(new StartOnClick()); } class StartOnClick implements OnClickListener { @Override public void onClick(View v) { progressbar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } Handler updateBarHandler = new Handler() { @Override public void handleMessage(Message msg) { progressbar.setProgress(msg.arg1); updateBarHandler.post(updateThread); } }; Runnable updateThread = new Runnable() { int i = 0; @Override public void run() { System.out.println("Begin Thread!" + i); i = i + 10; Message msg = updateBarHandler.obtainMessage(); msg.arg1 = i; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } updateBarHandler.sendMessage(msg); if (i == 100) { updateBarHandler.removeCallbacks(updateThread); } } }; }
實例三:
Handler與線程的關系(一)
XML布局代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="前進吧,小摩托!" /> </LinearLayout>
Java代碼:
public class HandlerThreadActivity extends Activity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { handler.post(r); setContentView(R.layout.activity_handlerthread); System.out.println("activity--->"+Thread.currentThread().getId()); System.out.println("activityname--->"+Thread.currentThread().getName()); } Runnable r = new Runnable(){ @Override public void run() { System.out.println("handler--->"+Thread.currentThread().getId()); System.out.println("handlername--->"+Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }
實例四:
Handler與線程的關系(二)
XML布局代碼:
同【實例三】中的布局代碼
Java代碼:
public class HandlerThreadActivity extends Activity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlerthread); Thread t = new Thread(r); t.start(); System.out.println("activity--->" + Thread.currentThread().getId()); System.out.println("activityname--->" + Thread.currentThread().getName()); } Runnable r = new Runnable() { @Override public void run() { System.out.println("handler--->" + Thread.currentThread().getId()); System.out.println("handlername--->" + Thread.currentThread().getName()); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }; }
實例五:
Handler實現真正的異步消息處理,在新線程中處理消息
XML布局代碼:
同【實例三】中的布局代碼
Java代碼:
public class HandlerMsgActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlermsg); // 打印當前線程 System.out.println("activity---->" + Thread.currentThread().getId()); // 生成一個HandlerThread對象(具有循環處理消息的功能),實現了Looper來處理消息隊列 HandlerThread handlerThread = new HandlerThread("handler_thread"); // 讓一個線程運行,必須用[線程名稱].start()方法 handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); // 將msg發動到目標對象,所謂目標對象,就是生成msg的handler對象 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override // 每當向Handler發送一個msg的時候,就會執行handleMessage()方法 public void handleMessage(Message msg) { System.out.println("handler---->" + Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }
實例六:
Handler實現真正的異步消息處理,在新線程中處理消息
XML布局代碼:
同【實例三】中的布局代碼
Java代碼:
public class HandlerMsgActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initContent(); } private void initContent() { setContentView(R.layout.activity_handlermsg); // 打印當前線程 System.out.println("activity---->" + Thread.currentThread().getId()); // 生成一個HandlerThread對象(具有循環處理消息的功能),實現了Looper來處理消息隊列 HandlerThread handlerThread = new HandlerThread("handler_thread"); // 讓一個線程運行,必須用[線程名稱].start()方法 handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); Bundle b = new Bundle(); b.putInt("age", 20); b.putString("name", "滔滔1024"); msg.setData(b); //msg.arg1與msg.arg2的消耗比msg.setData()小,但只能傳遞整型變量 // 將msg發動到目標對象,所謂目標對象,就是生成msg的handler對象,本例中是發送到MyHandler對象中 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override // 每當向Handler發送一個msg的時候,就會執行handleMessage()方法 public void handleMessage(Message msg) { Bundle b = msg.getData(); int age = b.getInt("age"); String name = b.getString("name"); System.out.println("age->"+age+"\nname->"+name); System.out.println("handler---->" + Thread.currentThread().getId()); System.out.println("handlerMessage"); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。