成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),白山企業(yè)網(wǎng)站建設(shè),白山品牌網(wǎng)站建設(shè),網(wǎng)站定制,白山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,白山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
點(diǎn)擊Start按鈕,每隔3秒在Logcat里打印一句話
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); } }; }
點(diǎn)擊Start按鈕之后每隔11000毫秒進(jìn)度條增加10%
界面 activity_main.xml
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()); } //當(dāng)點(diǎn)擊StartButton按鈕時(shí)就會執(zhí)行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); } } //使用匿名內(nèi)部類來復(fù)寫Handler當(dāng)中的handleMessage方法 Handler updateBarHandler = new Handler(){ public void handleMessage(android.os.Message msg) { bar.setProgress(msg.arg1); updateBarHandler.post(updateThread); }; }; //線程類,使用匿名內(nèi)部類的方式進(jìn)行聲明 Runnable updateThread = new Runnable() { int i = 0; @Override public void run() { System.out.println("Begin Thread"); i = i + 10; //得到一個(gè)消息對象。Message類是由Android操作系統(tǒng)提供。 Message msg = updateBarHandler.obtainMessage(); //使用arg1和arg2這兩個(gè)成員變量傳遞消息,優(yōu)點(diǎn)是節(jié)省系統(tǒng)資源 msg.arg1 = i; try { //設(shè)置當(dāng)前線程睡眠一秒 Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } //將msg加入到消息隊(duì)列當(dāng)中 updateBarHandler.sendMessage(msg); if (i== 100) { System.out.println(" i ==100"); updateBarHandler.removeCallbacks(updateThread); } } }; }
可以看到處理消息的線程和主線程是同一個(gè)線程
public class MainActivity extends ActionBarActivity { private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); handler.post(runnable); /*如果采用這種方式則會創(chuàng)建新的線程 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處理線程的時(shí)候并沒有創(chuàng)建新的線程 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) { } } }; }
在新的線程中處理消息,真正實(shí)現(xiàn)異步消息處理
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //打印當(dāng)前線程的ID System.out.println("MainActivity>>>" + Thread.currentThread().getId()); //生成一個(gè)HandlerThread對象,實(shí)現(xiàn)了使用Looper來處理消息隊(duì)列的功能 HandlerThread handlerThread = new HandlerThread("handler_Thread"); handlerThread.start(); MyHandler myHandler = new MyHandler(handlerThread.getLooper()); Message msg = myHandler.obtainMessage(); //msg.obj = "abc"; //簡單數(shù)據(jù) Bundle bundle = new Bundle(); bundle.putInt("age", 20); bundle.putString("name", "umgsai"); msg.setData(bundle); //將msg發(fā)送到目標(biāo)對象,即生成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; //取出數(shù)據(jù) 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"); } } }