您好,登錄后才能下訂單哦!
點擊Start按鈕,每隔3秒在Logcat里打印一句話
<Button android:id="@+id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start"/> <Button android:id="@+id/endButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/startButton" android:text="End"/>
MainActivity.java
public class MainActivity extends ActionBarActivity { private Button startButton = null; private Button endButton = null; Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.startButton); endButton = (Button) findViewById(R.id.endButton); startButton.setOnClickListener(new StartButtonListener()); endButton.setOnClickListener(new EndButtonListener()); } class StartButtonListener implements OnClickListener{ @Override public void onClick(View v) { handler.post(updateThread); } } class EndButtonListener implements OnClickListener{ @Override public void onClick(View v) { handler.removeCallbacks(updateThread); } } Runnable updateThread = new Runnable() { @Override public void run() { System.out.println("updateThread"); handler.postDelayed(updateThread, 3000); } }; }
點擊Start按鈕之后每隔11000毫秒進度條增加10%
界面 activity_main.xml
<ProgressBar android:id="@+id/bar" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone"/> <Button android:id="@+id/startButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/bar" android:text="Start"/>
MainActivity.java
public class MainActivity extends ActionBarActivity { private ProgressBar bar = null; private Button startButton = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bar = (ProgressBar) findViewById(R.id.bar); startButton = (Button) findViewById(R.id.startButton); startButton.setOnClickListener(new ButtonListener()); } //當點擊StartButton按鈕時就會執行ButtonListener的onClick方法 class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { System.out.println("Onclick"); Toast.makeText(MainActivity.this, "Onclick", Toast.LENGTH_SHORT).show(); bar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } //使用匿名內部類來復寫Handler當中的handleMessage方法 Handler updateBarHandler = new Handler(){ public void handleMessage(android.os.Message msg) { bar.setProgress(msg.arg1); updateBarHandler.post(updateThread); }; }; //線程類,使用匿名內部類的方式進行聲明 Runnable updateThread = new Runnable() { int i = 0; @Override public void run() { System.out.println("Begin Thread"); i = i + 10; //得到一個消息對象。Message類是由Android操作系統提供。 Message msg = updateBarHandler.obtainMessage(); //使用arg1和arg2這兩個成員變量傳遞消息,優點是節省系統資源 msg.arg1 = i; try { //設置當前線程睡眠一秒 Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } //將msg加入到消息隊列當中 updateBarHandler.sendMessage(msg); if (i== 100) { System.out.println(" i ==100"); updateBarHandler.removeCallbacks(updateThread); } } }; }
可以看到處理消息的線程和主線程是同一個線程
public class MainActivity extends ActionBarActivity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler.post(runnable); /*如果采用這種方式則會創建新的線程 Thread thread = new Thread(runnable); thread.start(); */ setContentView(R.layout.activity_main);// 要等10秒鐘才顯示 System.out.println("Activity:" + Thread.currentThread().getId()); System.out.println("ActivityName:" + Thread.currentThread().getName()); } // 上面打印的信息和下面打印的信息是一樣的 // Handler處理線程的時候并沒有創建新的線程 Runnable runnable = 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 (Exception e) { } } }; }
在新的線程中處理消息,真正實現異步消息處理
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打印當前線程的ID System.out.println("MainActivity>>>" + Thread.currentThread().getId()); //生成一個HandlerThread對象,實現了使用Looper來處理消息隊列的功能 HandlerThread handlerThread = new HandlerThread("handler_Thread"); handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); //msg.obj = "abc"; //簡單數據 Bundle bundle = new Bundle(); bundle.putInt("age", 20); bundle.putString("name", "umgsai"); msg.setData(bundle); //將msg發送到目標對象,即生成msg對象的Handler對象 msg.sendToTarget(); } class MyHandler extends Handler { public MyHandler() { } public MyHandler(Looper looper) { super(looper); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); //String s = (String) msg.obj; //取出數據 Bundle bundle = msg.getData(); System.out.println("age:" + bundle.getInt("age") + ",name:" + bundle.getString("name")); System.out.println("Handler>>>" + Thread.currentThread().getId()); System.out.println("handlerMeaage"); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。