• AsyncTask异步类的简单操作


    package com.example.day9;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    import android.app.Activity;
    import android.app.Dialog;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        private Button bt1;
        private ProgressDialog dialog;
        private String url="http://b.hiphotos.baidu.com/image/w%3D2048/sign=0240c37eb0119313c743f8b051000dd7/e4dde71190ef76c6f6ecf6979f16fdfaaf51674b.jpg";
        private ImageView imageView;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.imageView=(ImageView)super.findViewById(R.id.iv);
            this.dialog=new ProgressDialog(this);
            this.dialog.setTitle("提示框");
            this.dialog.setMessage("正在下载图片.."); 
            this.bt1=(Button)super.findViewById(R.id.bt1);
            this.bt1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    new ImageDowload().execute(url);
                }
            });
            
            
            
        }
         public class ImageDowload extends AsyncTask<String, Void, Bitmap>{
             //第一个参数为任务,这里指的是下载链接
             //第二个参数是进度,一般是Integer
             //第三个参数是执行结果返回类型,这里是图片
             public Bitmap doInBackground(String... arg0) {
                // 耗时操作在这里执行,参数为任务类型,这里是指下载链接(String)
                HttpClient client=new DefaultHttpClient();
                HttpGet httpGet=new HttpGet(arg0[0]);
                Bitmap bitmap=null;
                Log.i("lcw", arg0[0]);
                try {
                    HttpResponse httpResponse = client.execute(httpGet);
                    if(httpResponse.getStatusLine().getStatusCode()==200){
                        Log.i("www", httpResponse+"");
                        HttpEntity entity=httpResponse.getEntity();
                        byte data[]=EntityUtils.toByteArray(entity);
                        bitmap=BitmapFactory.decodeByteArray(data, 0, data.length);    
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                return bitmap;
            }
    
            @Override
            public void onPostExecute(Bitmap result) {
                // 最后操作,更新UI操作
                super.onPostExecute(result);
                Log.i("11111111", "eeee");
                MainActivity.this.imageView.setImageBitmap(result);
                dialog.dismiss();
            }
    
            @Override
            public void onPreExecute() {
                // 最早操作,预处理动作
                super.onPreExecute();
                MainActivity.this.dialog.show();
            }
            
            
            
            
        }
    
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context="com.example.day9.MainActivity"
        tools:ignore="MergeRootFrame" >
    
       <ImageView
           android:id="@+id/iv"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
            />
     
        <Button 
            android:id="@+id/bt1"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
            android:text="点击下载图片"
            />
        
    </RelativeLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.day9"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="19" />
        <uses-permission android:name="android.permission.INTERNET"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.example.day9.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>

    效果图如下:

  • 相关阅读:
    MySQL同步故障:" Slave_SQL_Running:No" 两种解决办法
    mysql运维-slave_skip_errors
    linux 的mysql 主从备份
    linux 查看和设置主机名
    mysql连接数
    红帽6.9搭建yum源的2种方式(HTTP和本地)
    linux 重启服务器命令
    Linux开机启动顺序启动顺序及配置开机启动
    在Windows Server 2012的Task Scheduler里面配置自动发送邮件
    "Cannot find one of more components. Please reinstall the application"--安装VS2013之后不能正常打开的处理办法
  • 原文地址:https://www.cnblogs.com/lichenwei/p/3684406.html
Copyright © 2020-2023  润新知