日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

android 之多线程详解

發(fā)布時(shí)間:2025/4/5 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 之多线程详解 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 中進(jìn)程的生命周期

<1>前臺(tái)進(jìn)程(Foreground process):正在運(yùn)行著和用戶交互的程序的進(jìn)程

<2>可見(jiàn)進(jìn)程(visible process):運(yùn)行著處于可見(jiàn)狀態(tài)組件的進(jìn)程

<3>服務(wù)進(jìn)程(Service process):運(yùn)行著使用startService()方法的服務(wù)同時(shí)沒(méi)有變?yōu)楦鼉蓚€(gè)級(jí)別的進(jìn)程

<4>后臺(tái)進(jìn)程(Background process):運(yùn)行著處于后臺(tái)組件的進(jìn)程,執(zhí)行了Activity的onStop()方法后的進(jìn)程

<5>空進(jìn)程(Empty Process):不包含任何活動(dòng)組建的進(jìn)程,它們的存在是為了緩存的目的,能夠加速重新啟動(dòng)當(dāng)前程序的速度。




UI線程(主線程)模型的兩條規(guī)則:

1.不要阻塞UI線程,否則超過(guò)5s會(huì)出現(xiàn)ANR(Applocation Not Responding)錯(cuò)誤的危險(xiǎn)。

2.不要在非UI線程中更新UI,否則直接報(bào)錯(cuò)。




解決UI線程模型兩條規(guī)則之間的矛盾,提供了三種方式

A.把主線程請(qǐng)到子線程的家里面,通過(guò)調(diào)用Activity的runOnUiThread(new Runnable(){ ?重寫run方法 }} ); ??

? ?或者是view.post(new Runnable()){ 重寫run方法 }} );

1.Activity.runOnUiThread(Runnable)

2.View.post(Runnable)

3.View.postDelayed(Runnable,long)

B.當(dāng)子線程有更新UI需要的時(shí)候,當(dāng)需要更新數(shù)據(jù)封裝到消息對(duì)象中并反送給主線程進(jìn)行更新UI(Handler機(jī)制) ?以后補(bǔ)充!

C.使用Google提供的AsyncTask(異步)解決。



? ? AsyncTask的特點(diǎn):

? ? 掌握如何構(gòu)建AsyncTask子類:

? ? 掌握AsyncTask<String,Integer,String>三個(gè)泛型參數(shù)的說(shuō)明:

<1>第一個(gè)參數(shù):指定當(dāng)我們啟動(dòng)異步任務(wù)(execute()方法實(shí)參的數(shù)據(jù)類型)時(shí)傳遞給doInBackground(String...parms)

? ? ? 方法的形參的數(shù)據(jù)類型加上。。。

<2>第二個(gè)參數(shù):指定對(duì)外報(bào)告進(jìn)度時(shí)調(diào)用方法publish Progress()中實(shí)參的數(shù)據(jù)類型。。。也是?

? ? ? ? ? onProgressUpdate(Integer...values)方法形參的數(shù)據(jù)類型。。。

<3>第三個(gè)參數(shù):指定doInBackground(String...parms)方法返回值的數(shù)據(jù)類型,也是onPostExcute(String s)方法形參的數(shù)據(jù)類型


package com.hsj.example.asynctaskdemo01;import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView;import java.util.Arrays;/*** https://www.baidu.com/img/bd_logo1.png?qua=high&where=super*/ public class MainActivity_bak04 extends AppCompatActivity {private TextView textView_info;private ProgressDialog progressDialog;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.textView_info= (TextView) this.findViewById(R.id.textView_info);//實(shí)例化對(duì)話框?qū)ο髏his.progressDialog=new ProgressDialog(this);this.progressDialog.setMax(100);//將對(duì)話框樣式設(shè)置成水平進(jìn)度對(duì)話框this.progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//當(dāng)執(zhí)行execute()方法時(shí)會(huì)自動(dòng)調(diào)用doInBackground(Void... params)new MyAsyncTask().execute("http://www.baidu.com","http://www.163.com");// new MyAsyncTask().execute("http://www.baidu.com","http://www.163.com");//MyAsyncTask myAsyncTask=new MyAsyncTask();//myAsyncTask.execute("http://www.baidu.com","http://www.163.com");/*異步任務(wù)不能多次啟動(dòng),否則報(bào)異常:Cannot execute task: the task is already running*///myAsyncTask.execute("http://www.baidu.com","http://www.163.com");}/*** AsyncTask<String,Integer,String>三個(gè)泛型參數(shù)說(shuō)明:* 第一個(gè)參數(shù):指定當(dāng)我們啟動(dòng)異步任務(wù)(execute()方法實(shí)參的數(shù)據(jù)類型)時(shí)傳遞給doInBackground(String... params)方法的形參的數(shù)據(jù)類型加上...* 第二個(gè)參數(shù):指定對(duì)外報(bào)告進(jìn)度時(shí)調(diào)用方法publishProgress()中實(shí)參的數(shù)據(jù)類型....,也是onProgressUpdate(Integer... values)方法形參的數(shù)據(jù)類型...* 第三個(gè)參數(shù):指定doInBackground(String... params)方法返回值的數(shù)據(jù)類型,也是onPostExecute(String s)方法形參的數(shù)據(jù)類型**/private final class MyAsyncTask extends AsyncTask<String,Integer,String>{@Overrideprotected void onPreExecute() {//設(shè)置進(jìn)度對(duì)話框的提示消息progressDialog.setMessage("開(kāi)始下載網(wǎng)絡(luò)圖片......");//顯示進(jìn)度對(duì)話框progressDialog.show();System.out.println("===onPreExecute()===");}@Overrideprotected String doInBackground(String... params) {System.out.println("===doInBackground(String... params="+params+")==");System.out.println("params="+ Arrays.toString(params));//publishProgress(10,20,30);//報(bào)告進(jìn)度的值會(huì)傳遞給onProgressUpdate(Integer... values)方法的形參valuesfor(int i=0;i<100;i++){publishProgress(i);if(i==50){System.out.println("===用戶取消了下載====");//取消執(zhí)行耗時(shí)操作this.cancel(true);}try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}}return "網(wǎng)絡(luò)下載數(shù)據(jù)完畢";}@Overrideprotected void onProgressUpdate(Integer... values) {progressDialog.setProgress(values[0]);System.out.println("===onProgressUpdate(Integer... values="+Arrays.toString(values)+")===");}/*** 當(dāng)用戶執(zhí)行了取消異步任務(wù)的操作后自動(dòng)調(diào)用的方法,此時(shí)就不再調(diào)用onPostExecute(String s)* 當(dāng)前方法也是運(yùn)行在主線程中*/@Overrideprotected void onCancelled() {//銷毀進(jìn)度對(duì)話框progressDialog.dismiss();String threadName=Thread.currentThread().getName();System.out.println("===onCancelled().threadName="+threadName);}/*** 當(dāng)正常執(zhí)行完畢異步任務(wù)中的doInBackground()方法后指定調(diào)用的方法* @param s*/@Overrideprotected void onPostExecute(String s) {//銷毀進(jìn)度對(duì)話框progressDialog.dismiss();System.out.println("===onPostExecute(String s="+s+")====");}}}




示例代碼

package com.hsj.example.asynctaskdemo01;import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView textView_info;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);this.textView_info= (TextView) this.findViewById(R.id.textView_info);}/*** android 單線程模型的內(nèi)容:* 1.不要阻塞UI線程,因?yàn)槌^(guò)5秒后可能會(huì)出現(xiàn)ANR(Application Not Response) 應(yīng)用程序無(wú)響應(yīng)錯(cuò)誤* 2.不要在子線程中直接更新UI,否則會(huì)直接報(bào)錯(cuò)*** 為了解決android單線程模型的問(wèn)題,我們提供了三種解決方案:* A:把主線程請(qǐng)到子線程的家里來(lái)* 1. Activity.runOnUiThread(Runnable)*2. View.post(Runnable)3. View.postDelayed(Runnable, long)** B:使用Handler機(jī)制(當(dāng)子線程有更新UI的意愿時(shí),把需要更新的數(shù)據(jù)封裝成消息傳遞到主線程中在更新UI),將在第十八章詳解** C:使用異步任務(wù)解決,這章的重點(diǎn).** @param view*/public void click(View view){/*在java 中開(kāi)啟子線程有兩種方式:A:繼承Thread 類B:實(shí)現(xiàn)Runnable 接口*/new Thread(){@Overridepublic void run() {String threadName=Thread.currentThread().getName();System.out.println("threadName="+threadName);//模擬耗時(shí)操作,比如網(wǎng)絡(luò)下載/* try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}*/final String info="你點(diǎn)我的后果看到了嗎?";/*//在子線程中開(kāi)辟一塊主線程的執(zhí)行區(qū)域,方式一:runOnUiThread(new Runnable() {*//*** run()方法就運(yùn)行在主線程中了*//*@Overridepublic void run() {String myName=Thread.currentThread().getName();System.out.println("Runnable.myName="+myName);//更新UI,在內(nèi)部類中訪問(wèn)外部類的局部變量則需要將局部變量變成常量textView_info.setText(info);}});*//*//方式二:textView_info.post(new Runnable() {@Overridepublic void run() {String myName=Thread.currentThread().getName();System.out.println("Runnable.myName="+myName);//更新UI,在內(nèi)部類中訪問(wèn)外部類的局部變量則需要將局部變量變成常量textView_info.setText(info);}});*///方式三:postDelayed(runnable,long):延遲long 毫秒后執(zhí)行run()方法textView_info.postDelayed(new Runnable() {@Overridepublic void run() {String myName=Thread.currentThread().getName();System.out.println("Runnable.myName="+myName);//更新UI,在內(nèi)部類中訪問(wèn)外部類的局部變量則需要將局部變量變成常量textView_info.setText(info);}},2000);}}.start();} }

異步任務(wù)的實(shí)例:

點(diǎn)擊打開(kāi)鏈接


總結(jié)

以上是生活随笔為你收集整理的android 之多线程详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。