編輯:關於Android編程
1.第一種方法
package com.example.jer824; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView tv; private TextView tv1; int a=100; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1){ Log.d("??????",a+""); tv.setText(a+""); a--; Log.d("??????","成功"); new GetCache(a).run(); }else{tv.setText("倒計時結束");} } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView)findViewById(R.id.tv); tv1=(TextView)findViewById(R.id.tv1); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new GetCache(a).run(); } }); } private class GetCache implements Runnable { //兩種方法繼承或接口 private int id; public GetCache(int id) { this.id = id; } @Override public void run() { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Message message = new Message(); if (id==0) { message.what = 2; handler.sendMessage(message); }else { message.what = 1; handler.sendMessage(message); } } } }
2.第二種方法
package com.example.jer824; import android.os.Bundle; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Main2Activity extends AppCompatActivity { private TextView tv; private Button bt; //線程間通信 private android.os.Handler handler=new android.os.Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); tv.setText(msg.what+""); Bundle bundle=msg.getData(); String str=bundle.getString("a"); tv.setText(str+msg.what); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); tv= (TextView) findViewById(R.id.tv); bt= (Button) findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new GetCache()).start(); // new Thread(new Runnable() { // @Override // public void run() { // int a=100; // while (a>=0){ // try { // Thread.sleep(100); // } catch (InterruptedException e) { // e.printStackTrace(); // } // a--; // Message message=new Message(); // message.what = a; // handler.sendMessage(message); // } // } // }).start(); } }); } //多線程 子線程 //public class GetCache extends Thread public class GetCache implements Runnable{ @Override public void run() { //子線程運行完了,去主線程 int a=100; while (a>=0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } a--; Message message=new Message(); message.what = a; Bundle bundle=new Bundle(); bundle.putString("a","AAA"); message.setData(bundle); // handler.sendMessageDelayed(message,200); handler.sendMessage(message); //與上面三行一個意思 // handler.sendEmptyMessage(a); } } } }
package com.example.jer824; import android.os.Bundle; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class Main3Activity extends AppCompatActivity { private TextView tv; private Button bt; private int count=100; //線程間通信 private android.os.Handler handler=new android.os.Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //tv.setText(msg.what); if(count==0){ return; } tv.setText(msg.what + ""); handler.sendEmptyMessageDelayed(count--,500); //tv.setText(msg.what + ""); //bundle傳值 // Bundle bundle=msg.getData(); // String str = bundle.getString("a"); // tv.setText(str+msg.what); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); tv= (TextView) findViewById(R.id.tv); bt= (Button) findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessage(count); // // new Thread(new Runnable() { // @Override // public void run() { // int a=100; // while (a>0){ // try { // Thread.sleep(100); // } catch (InterruptedException e) { // e.printStackTrace(); // } // a--; // // tv.setText(a+""); // Message message=new Message(); // message.what = a; // handler.sendMessage(message); // } // } // }).start(); //new Thread(new GetCache()).start(); } }); } //多線程 子線程 //public class GetCache extends Thread public class GetCache implements Runnable{ @Override public void run() { //子線程運行完了,去主線程 int a=100; while (a>0){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } a--; // tv.setText(a+""); Message message=new Message(); message.what = a; //加入bundle值 // Bundle bundle=new Bundle(); // bundle.putString("a","1"); // message.setData(bundle); handler.sendMessage(message); //多久後啟動 //handler.sendMessageAtTime(message,500); //handler.sendMessageDelayed(message,500); //handler.sendEmptyMessage(a);傳空值 } } } }
使用Vitamio打造自己的Android萬能播放器(1)——准備一、實現目標1.1亮度控制模仿VPlayer界面:1.2聲音控制模
很久以前寫過兩篇Fragment的介紹,主要就是介紹其功能:Android Fragment 真正的完全解析(上)和Android Fragment 真正的
出於性能優化考慮,android的UI操作並不是線程安全的,這意味著意味著如果有多個線程並發操作UI組件,可能導致線程安全問題,未解決此問題, 從開發的角度來說, Han
最近在學習AIOSO(Alibaba Internal Open Source Organization,即阿裡巴巴內部開源組織) 的一個子項目MMCherryUI,這是