static_cast,dynamic_cast,reinterpret_cast,const_cast的差別及用法詳解。本站提示廣大學習愛好者:(static_cast,dynamic_cast,reinterpret_cast,const_cast的差別及用法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是static_cast,dynamic_cast,reinterpret_cast,const_cast的差別及用法詳解正文
本文實例講述了Android應用AsyncTask完成多線程下載的辦法。分享給年夜家供年夜家參考,詳細以下:
public class MainActivity extends Activity implements OnClickListener { private Button btn1, btn2, btn3; private ProgressBar progressBar1, progressBar2, progressBar3; private ImageView img1, img2, img3; private static final String IMG_URI = "http://www.jb51.net/images/logo.gif"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); } /** * 初始化監聽器 */ private void initListener() { btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); } /** * * 初始化控件 */ private void initView() { btn1 = (Button) findViewById(R.id.btn1); btn2 = (Button) findViewById(R.id.btn2); btn3 = (Button) findViewById(R.id.btn3); progressBar1 = (ProgressBar) findViewById(R.id.progressbar1); progressBar2 = (ProgressBar) findViewById(R.id.progressbar2); progressBar3 = (ProgressBar) findViewById(R.id.progressbar3); img1 = (ImageView) findViewById(R.id.img1); img2 = (ImageView) findViewById(R.id.img2); img3 = (ImageView) findViewById(R.id.img3); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View arg0) { // 點擊按鈕模仿下載 MyDownloadAsy down = new MyDownloadAsy(); down.execute(IMG_URI, arg0.getId() + ""); } /** * 1.Params,傳遞給後台義務的參數類型。 * * 2.Progress,後台盤算履行進程中,提高單元(progress units)的類型。(就是後台法式曾經履行了百分之幾了。) * * 3.Result, 後台履行前往的成果的類型。 */ class MyDownloadAsy extends AsyncTask<String, Integer, Bitmap> { private static final String TAG = "asy"; private int clickBtn = 0; private void i(String object) { Log.i(TAG, object); } @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); i("準備運行線程"); progressBar1.setProgress(0);// 進度條復位 progressBar2.setProgress(0);// 進度條復位 progressBar3.setProgress(0);// 進度條復位 } @Override protected Bitmap doInBackground(String... arg0) { switch (Integer.parseInt(arg0[1])) { case R.id.btn1: clickBtn = 1; break; case R.id.btn2: clickBtn = 2; break; case R.id.btn3: clickBtn = 3; break; default: break; } i("正在後台履行"); publishProgress(0); // 下載圖片 HttpClient hc = new DefaultHttpClient(); // 期待2s sleepWait(); publishProgress(50); HttpGet hg = new HttpGet(arg0[0]);// 獲得jb51的logo final Bitmap bm; try { HttpResponse hr = hc.execute(hg); bm = BitmapFactory.decodeStream(hr.getEntity().getContent()); } catch (Exception e) { return null; } sleepWait(); publishProgress(100); // mImageView.setImageBitmap(result); 不克不及在後台線程操作ui return bm; } /** * 期待2s鐘 */ private void sleepWait() { try { Thread.sleep(2000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } protected void onProgressUpdate(Integer... values) { // 靜態更新 i("進度更新"); switch (clickBtn) { case 1: progressBar1.setProgress(values[0]);// 更新進度條的進度 break; case 2: progressBar2.setProgress(values[0]);// 更新進度條的進度 break; case 3: progressBar3.setProgress(values[0]);// 更新進度條的進度 break; default: break; } } protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); i("線程履行完成"); if (result != null) { i("下載圖片勝利"); switch (clickBtn) { case 1: img1.setImageBitmap(result); break; case 2: img2.setImageBitmap(result); break; case 3: img3.setImageBitmap(result); break; default: break; } } else { i("下載圖片掉敗"); } } @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); i("撤消線程"); switch (clickBtn) { case 1: progressBar1.setProgress(0);// 進度條復位 break; case 2: progressBar2.setProgress(0);// 進度條復位 break; case 3: progressBar3.setProgress(0);// 進度條復位 break; default: break; } } } }
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerHorizontal="true" > <TableRow> <Button android:id="@+id/btn1" android:text="@string/btn1" /> <ProgressBar android:id="@+id/progressbar1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img1" android:src="@drawable/ic_launcher" /> </TableRow> <TableRow> <Button android:id="@+id/btn2" android:text="@string/btn2" /> <ProgressBar android:id="@+id/progressbar2" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img2" android:src="@drawable/ic_launcher" /> </TableRow> <TableRow> <Button android:id="@+id/btn3" android:text="@string/btn3" /> <ProgressBar android:id="@+id/progressbar3" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@id/img3" android:src="@drawable/ic_launcher" /> </TableRow> </TableLayout> </RelativeLayout>
AndroidManifast:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xunfang.asynctackdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" /> <!-- 拜訪收集的權限 --> <uses-permission android:name="android.permission.INTERNET" > </uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.xunfang.asynctackdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
願望本文所述對年夜家Android法式設計有所贊助。