下载之前的准备工作

来源:互联网 发布:网页下载app源码 编辑:程序博客网 时间:2024/05/17 20:34

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.studio.asynctaskproject">    <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

activity_main.xml


<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:orientation="vertical"    tools:context="com.studio.asynctaskproject.MainActivity">    <ProgressBar        android:id="@+id/progressBar"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="match_parent"        android:layout_height="15dp" />    <Button        android:id="@+id/button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="点击下载" />    <TextView        android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Hello world! imooc" /></LinearLayout>

MainActivity.java


package com.studio.asynctaskproject;import android.os.AsyncTask;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ProgressBar;import android.widget.TextView;/** * 1. 网络上请求数据:申请网络权限 读写存储权限 * 2. 布局我们的layout * 3. 下载前我们要做什么?  UI * 4. 下载中我们要做什么?  数据 * 5. 下载后我们要做什么?  UI */public class MainActivity extends AppCompatActivity {    public static final int INIT_PROGRESS = 0;    private ProgressBar mProgressBar;    private Button mDownloadButton;    private TextView mResultTextView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化视图        initView();        //设置点击监听        setListener();        //初始化UI数据        setData();    }    /**     * 初始化视图     */    private void initView() {        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);        mDownloadButton = (Button) findViewById(R.id.button);        mResultTextView = (TextView) findViewById(R.id.textView);    }    private void setListener() {        mDownloadButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                // TODO: 2017/8/20 下载任务            }        });    }    private void setData() {        mProgressBar.setProgress(INIT_PROGRESS);        mDownloadButton.setText(R.string.click_download);        mResultTextView.setText(R.string.download_text);    }    private class DownloadAsyncTask extends AsyncTask<String, Integer, Boolean> {        private static final String TAG = "MainActivity";        /**         * 在异步任务之前,在主线程中         */        @Override        protected void onPreExecute() {            super.onPreExecute();            //可操作UI  类似淘米,之前的准备工作            mDownloadButton.setText(R.string.downloading);            mResultTextView.setText(R.string.downloading);        }        /**         * 在另外一个线程中处理事件         *         * @param strings 入参         * @return 结果         */        @Override        protected Boolean doInBackground(String... strings) {            for (int i = 0; i < 10000; i++) {                Log.i(TAG, "doInBackground: " + strings[0]);                //抛出进度                publishProgress(i);            }            try {                Thread.sleep(100000);            } catch (InterruptedException e) {                e.printStackTrace();            }            return true;        }        @Override        protected void onPostExecute(Boolean aBoolean) {            super.onPostExecute(aBoolean);            //也是在主线程中,执行结果 处理        }        @Override        protected void onProgressUpdate(Integer... values) {            super.onProgressUpdate(values);            //收到进度,然后处理:  也是在UI线程中        }    }}
原创粉丝点击