編輯:關於android開發
我們在使用手機的時候,經常會遇到一個問題:先是卡死,然後跳出該程序無響應,是否關閉的提示(當然有可能是我們手機性能太差=。=)這是因為線程的阻塞引起的,在這裡我講述一下UI線程,一般處理程序會在UI線程中執行耗時操作,這回導致UI線程阻塞,當UI線程阻塞,屏幕會出現卡死,用戶體驗會變得非常差,當線程阻塞超過5s,android系統可能進行干預,彈出對話框詢問是否關閉。那如何解決呢?
解決方案一:創建一個新線程
我在UI視圖中創建了一個button和一個textView
Button button=(Button)findViewById (R.id.button);
TextView textView=(TextView)findViewById(R.id.textView);
TranslateAnimation animation=new TranslateAnimation(0,200,0,0);
animation.setRepeatCount(3);
animation.setDuration(2000);
textView.setAnimation(animation);
//這裡我讓textView在進入app時進行移動動畫
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {//監聽button的點擊
new Thread(new Runnable() {//創建一個新線程
@Override
public void run() {
try {
Thread.sleep(5000);//在這裡我讓線程進行耗時操作
}
catch (InterruptedException e){
e.printStackTrace();
}
}
}).start();
}
});
上面的代碼我創建一個新的線程來實現耗時,但是實際過程中進行的不可能只是一個耗時操作,讓我們在新線程中加兩句話,TextView view=(TextView)v;view.setText(""+100);(獲取到當前控件,並將其文字設置成100)現在讓我們再來試試這個程序,這個時候程序又報錯了
Only the original thread that created a view hierarchy can touch its views.
翻譯成中文就是:只有創建view的那個線程才能對其進行修改。
其實谷歌有兩條建議,也可以說是規矩
there are simply two rules to Android's single thread model:
Do not block the Ui thread//不要阻塞UI線程
Do not access the Android UI toolkit from outside the UI thread//不要在UI線程外的其他線程對視圖中的組件進行設置
那麼很多人就有疑問了,這不是矛盾了嗎?谷歌也為我們提供了解決方案
解決方案一:view.post
上面代碼出錯是因為我們在UI之外的線程調用了UI控件;那麼現在,我們在try{}catch(){}語句後增加一下代碼
1 v.post(new Runnable() {
2 @Override
3 public void run() {
4 TextView view=(TextView)v;
5 view.setText(""+sun);
6 }
7 });
這段代碼將我的語句提交到了UI線程中;但是view.post也有一些缺點
冗余,可讀性差,維護性差
為此官方也提供了另外一種解決方法
解決方法二:AsyncTask
AsyncTask和post方法大同小異
private class DownloadImageTask extends AsyncTask<String ,Void,Integer>{
protected Integer doInBackground(String...urls){
try{
Thread.sleep(5000);
}catch (InterruptedException e){
e.printStackTrace();
}
int sun=100;
return sun;
}
protected void onPostExecute(Integer sum){
button2.setText(""+sum);
}
}
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadImageTask().execute();
}
});
我們現在外部創建一個方法,然後在button的onClick事件中引用。
深入理解RCU實現
深入理解RCU實現深入理解RCU實現——基於內核2.6.21RCU實現(lvyilong316)RCU(Read-Copy Update),顧名思義就是讀-拷貝修改,它是
Android ApiDemos導入
Android ApiDemos導入 Android SDK中自帶有很多例子,存放在android-sdksamples目錄下,其中介紹了Android平台主要的API的
git詳解,git
git詳解,gitgit詳解 git是從android出現,就作為版本管理工具。由於很多人從svn開始使用,簡單的check in & check out操作,很
Android學習指南之二十二:實例講解AIDL和遠程Service調用
本講的內容,理解起來很難,也許你看了很多資料也看不明白,但是用起來缺簡單的要命