用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

异步线程

2016-10-19 作者: 永夜极光举报

[android]代码库

1.MainActivity.java

package com.example.acer.myapplication;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.AsyncTask;
    import android.view.View.OnClickListener;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.ProgressBar;
    import com.samir.R;


public class MainActivity extends AppCompatActivity {
        MyTask objMyTask;

        private static final int SLEEP_TIME=((10*1000)/100);
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

      public void  click(View view)
      {
          objMyTask = new MyTask();
          objMyTask.execute();
      }
    
        class MyTask extends AsyncTask<Void, Integer, Void> {
            Dialog dialog;
            ProgressBar progressBar;
            TextView tvLoading,tvPer;
            Button btnCancel;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = new Dialog(MainActivity.this);
                dialog.setCancelable(false);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.progressdialog);
                progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1);
                tvLoading = (TextView) dialog.findViewById(R.id.tv1);
                tvPer = (TextView) dialog.findViewById(R.id.tvper);
                btnCancel = (Button) dialog.findViewById(R.id.btncancel);
                btnCancel.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        objMyTask.cancel(true);
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {

                for (int i = 1; i <100; i++) {
                    if (isCancelled()) {break;} 
                    else {publishProgress(i);
                        try {Thread.sleep(SLEEP_TIME);} 
                        catch (InterruptedException e) {e.printStackTrace();}
                    }
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                progressBar.setProgress(values[0]);
                tvLoading.setText("Loading...  " + values[0] + " %");
                tvPer.setText(values[0]+" %");
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                dialog.dismiss();
                AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
                alert.setTitle("Completed!!!");
                alert.setMessage("Your Task is Completed SuccessFully!!!");
                alert.setButton("Dismiss", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alert.show();
            }
        }
    }

2.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#dcdcdc"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text="开始"
        android:onClick="click" />
</RelativeLayout>

3.progressdialog.xml

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:text="Loading..."
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv1"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="25dp"
        android:max="100" />

    <Button
        android:id="@+id/btncancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="Cancel" />

    <TextView
        android:id="@+id/tvper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/progressBar1"
        android:layout_centerHorizontal="true"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...