异步任务——AsyncTask的初步认识

 

clip_image002

 

clip_image004

clip_image006

clip_image007

clip_image009

 

ProgressBar_test.class
package com.example.administrator.ten_9;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * Created by Administrator on 2015/10/9 0009.
 */
public class ProgressBar_test extends Activity {
    private ProgressBar progressBar;
    private my my;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.progressbar);
        progressBar = (ProgressBar) findViewById(R.id.pb);
        textView    = (TextView) findViewById(R.id.textjindu);
        my = new my();
        my.execute();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //生命周期绑定
        //AsyncTask不为空而且是在Running的
        if (my != null && my.getStatus() == AsyncTask.Status.RUNNING){
            //cancel方法只是将对应的AsyncTask标记为cancel状态,并不是真正的取消线程执行。
            my.cancel(true);
        }
    }

    class my extends AsyncTask<Void,Integer,Void>{

        @Override
        protected Void doInBackground(Void... params) {
            //模拟进度更新
            for(int i = 0; i <= 100 ; i++){
                //判断是否被标记为 cancel 是 就break出来
                if(isCancelled()){
                    break;
                }
                publishProgress(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            Log.d("xys", String.valueOf(values[0]));
            //判断是否被标记为 cancel 是 就直接返回
            if(isCancelled()){
                return;
            }
            //获取更新进度
            textView.setText(values[0]+"");
            progressBar.setProgress(values[0]);
        }
    }
}

  

progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="16dp"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <ProgressBar
        android:layout_centerInParent="true"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/pb" />
    <TextView
        android:id="@+id/textjindu"
        android:gravity="center_horizontal"
        android:textSize="20sp"
        android:text="0"
        android:layout_below="@id/pb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</RelativeLayout>

  

MainActivity.class
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyAsyncTask myAsyncTask = new MyAsyncTask();
        //start 异步处理myAsyncTask
        myAsyncTask.execute();
    }

    public void LoadImage(View view){
        startActivity(new Intent(this,ImageTest.class));

    }
    public void LoadProgress(View view){
        startActivity(new Intent(this,ProgressBar_test.class));
    }
}

  mainactivity.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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:id="@+id/bt1"
        android:text="跳转"
        android:onClick="LoadImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:layout_below="@id/bt1"
        android:id="@+id/bt2"
        android:text="跳转2"
        android:onClick="LoadImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    
</RelativeLayout>

  

转载于:https://www.cnblogs.com/zmaibbs7/p/4864466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值