不是異步的例子,顯然有個(gè)延遲。
AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。
◆Params 啟動(dòng)任務(wù)執(zhí)行的輸入?yún)?shù),比如HTTP請求的URL。
◆Progress 后臺(tái)任務(wù)執(zhí)行的百分比。
◆Result 后臺(tái)執(zhí)行任務(wù)最終返回的結(jié)果,比如String。
AsyncTask的執(zhí)行分為四個(gè)步驟,每一步都對(duì)應(yīng)一個(gè)回調(diào)方法,這些方法不應(yīng)該由應(yīng)用程序調(diào)用,開發(fā)者需要做的就是實(shí)現(xiàn)這些方法。
onPreExecute(), 該方法將在執(zhí)行實(shí)際的后臺(tái)操作前被UI thread調(diào)用。可以在該方法中做一些準(zhǔn)備工作,如在界面上顯示一個(gè)進(jìn)度條。
doInBackground(Params...), 將在onPreExecute 方法執(zhí)行后馬上執(zhí)行,該方法運(yùn)行在后臺(tái)線程中。這里將主要負(fù)責(zé)執(zhí)行那些很耗時(shí)的后臺(tái)計(jì)算工作。可以調(diào)用 publishProgress方法來更新實(shí)時(shí)的任務(wù)進(jìn)度。該方法是抽象方法,子類必須實(shí)現(xiàn)。
onProgressUpdate(Progress...),在publishProgress方法被調(diào)用后,UI thread將調(diào)用這個(gè)方法從而在界面上展示任務(wù)的進(jìn)展情況,例如通過一個(gè)進(jìn)度條進(jìn)行展示。
onPostExecute(Result), 在doInBackground 執(zhí)行完成后,onPostExecute 方法將被UI thread調(diào)用,后臺(tái)的計(jì)算結(jié)果將通過該方法傳遞到UI thread.
為了正確的使用AsyncTask類,以下是幾條必須遵守的準(zhǔn)則:
1) Task的實(shí)例必須在UI thread中創(chuàng)建
2) execute方法必須在UI thread中調(diào)用
3) 不要手動(dòng)的調(diào)用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個(gè)方法
4) 該task只能被執(zhí)行一次,否則多次調(diào)用時(shí)將會(huì)出現(xiàn)異常
下面是AsyncTask異步獲取已安裝程序列表的例子:
simple_item_2.xml
Android得到系統(tǒng)已安裝應(yīng)用程序包列表方法 自定義ListView顯示 PackageManager的使用
AyncTask 實(shí)戰(zhàn) 模擬GridView 動(dòng)態(tài)更新效果
http://www.ophonesdn.com/article/show/80
package com.ql.app; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; public class ProcessorBarTest extends Activity { private ListView listview; private Context mContext; private List<String> mAppList; private ArrayAdapter mAdapter; private boolean mIsLoaded = false; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.screen_1); listview = (ListView)findViewById(R.id.listview); mContext = this; mAppList = new ArrayList<String>(); mAdapter = new ArrayAdapter(mContext,android.R.layout.simple_list_item_1, mAppList); listview.setAdapter(mAdapter); // 獲取已經(jīng)安裝程序列表 PackageManager pm = mContext.getPackageManager(); //有入口&圖標(biāo)的一定就是應(yīng)用 Intent intent = new Intent(Intent.ACTION_MAIN, null);//入口就是應(yīng)用 intent.addCategory(Intent.CATEGORY_LAUNCHER);//有圖標(biāo)的就是應(yīng)用? List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); for (int i=0; i<list.size(); i++) { mAppList.add(list.get(i).loadLabel(pm).toString()); } mAdapter.notifyDataSetChanged(); } }

AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。
◆Params 啟動(dòng)任務(wù)執(zhí)行的輸入?yún)?shù),比如HTTP請求的URL。
◆Progress 后臺(tái)任務(wù)執(zhí)行的百分比。
◆Result 后臺(tái)執(zhí)行任務(wù)最終返回的結(jié)果,比如String。
AsyncTask的執(zhí)行分為四個(gè)步驟,每一步都對(duì)應(yīng)一個(gè)回調(diào)方法,這些方法不應(yīng)該由應(yīng)用程序調(diào)用,開發(fā)者需要做的就是實(shí)現(xiàn)這些方法。
onPreExecute(), 該方法將在執(zhí)行實(shí)際的后臺(tái)操作前被UI thread調(diào)用。可以在該方法中做一些準(zhǔn)備工作,如在界面上顯示一個(gè)進(jìn)度條。
doInBackground(Params...), 將在onPreExecute 方法執(zhí)行后馬上執(zhí)行,該方法運(yùn)行在后臺(tái)線程中。這里將主要負(fù)責(zé)執(zhí)行那些很耗時(shí)的后臺(tái)計(jì)算工作。可以調(diào)用 publishProgress方法來更新實(shí)時(shí)的任務(wù)進(jìn)度。該方法是抽象方法,子類必須實(shí)現(xiàn)。
onProgressUpdate(Progress...),在publishProgress方法被調(diào)用后,UI thread將調(diào)用這個(gè)方法從而在界面上展示任務(wù)的進(jìn)展情況,例如通過一個(gè)進(jìn)度條進(jìn)行展示。
onPostExecute(Result), 在doInBackground 執(zhí)行完成后,onPostExecute 方法將被UI thread調(diào)用,后臺(tái)的計(jì)算結(jié)果將通過該方法傳遞到UI thread.
為了正確的使用AsyncTask類,以下是幾條必須遵守的準(zhǔn)則:
1) Task的實(shí)例必須在UI thread中創(chuàng)建
2) execute方法必須在UI thread中調(diào)用
3) 不要手動(dòng)的調(diào)用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個(gè)方法
4) 該task只能被執(zhí)行一次,否則多次調(diào)用時(shí)將會(huì)出現(xiàn)異常
下面是AsyncTask異步獲取已安裝程序列表的例子:
public class Screen1 extends Activity{ private static final String tag="Screen1"; private ListView listview; private Context mContext; private List<ResolveInfo> list; private AppAdapter adapter; private PackageManager pm; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //給Activity注冊界面進(jìn)度條功能 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.screen_1); mContext = this; list=new ArrayList<ResolveInfo>(); pm = mContext.getPackageManager(); listview = (ListView)findViewById(R.id.listview); adapter = new AppAdapter(mContext); listview.setAdapter(adapter); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Log.i(tag, "---------------onItemClick-----------------"); ResolveInfo info=list.get(position); String packageName = info.activityInfo.packageName; String className = info.activityInfo.name; Intent intent = new Intent(); intent.setClassName(packageName, className); startActivity(intent); } }); new MyTask().execute(); } class AppAdapter extends BaseAdapter{ Context context; AppAdapter(Context context){ this.context=context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if(convertView==null){ LayoutInflater inflater=getLayoutInflater().from(context); convertView=inflater.inflate(R.layout.simple_item_2, null); } ImageView iv=(ImageView)convertView.findViewById(R.id.icon); TextView tv=(TextView)convertView.findViewById(R.id.text); ResolveInfo info=list.get(position); iv.setBackgroundDrawable(info.activityInfo.loadIcon(pm)); tv.setText(info.activityInfo.loadLabel(pm)); return convertView; } } class MyTask extends AsyncTask<String, Integer, String>{ @Override protected void onPreExecute() { setProgressBarIndeterminateVisibility(true); showProgress(); } @Override protected void onPostExecute(String param) { setProgressBarIndeterminateVisibility(false); adapter.notifyDataSetChanged(); closeProgress(); } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub super.onProgressUpdate(values); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub // 獲取已經(jīng)安裝程序列表 Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); list = pm.queryIntentActivities(intent, 0); Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm)); return null; } } private Dialog dialog; protected void showProgress() { if(dialog == null) { dialog = new Dialog(this, R.style.Theme_TransparentDialog); // dialog.setContentView(R.layout.progress_dialog); dialog.setContentView(new ProgressBar(this)); dialog.setCancelable(true); dialog.show(); } } // protected void closeProgress() { if(dialog != null) { dialog.cancel(); dialog = null; } } public boolean isShowing(){ if(dialog != null) { return dialog.isShowing(); } return false; } }
simple_item_2.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="應(yīng)用列表" /> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>

Android得到系統(tǒng)已安裝應(yīng)用程序包列表方法 自定義ListView顯示 PackageManager的使用
AyncTask 實(shí)戰(zhàn) 模擬GridView 動(dòng)態(tài)更新效果
http://www.ophonesdn.com/article/show/80
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
