使用的AsyncTask在Android的显示进度 [英] Use Asynctask to display a ProgressBar in Android

查看:133
本文介绍了使用的AsyncTask在Android的显示进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做显示进度。

我是一个Android的初学者。

当我preSS按钮,任务应该在后台运行,但它不显示进度。

什么是问题?我无法理解。

请帮帮我!

MainActivity:

 包com.example.shikkok_services;

进口的java.util.ArrayList;

进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.Toast;

公共类MainActivity延伸活动{


    私人按钮btnThread,btntask;
    私人ProgressDialog PD;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        btnThread =(按钮)findViewById(R.id.btnStartThread);
        btntask =(按钮)findViewById(R.id.btntsk);

        btntask.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){


                //启动myTast

                新MyTask的(MainActivity.this).execute();


            }
        });



    }

}
 

MyTask的:

 包com.example.shikkok_services;

进口android.app.Dialog;
进口android.content.Context;
进口android.os.AsyncTask;
进口android.os.Handler;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.Window;
进口android.widget.Button;
进口android.widget.ProgressBar;
进口android.widget.TextView;
进口android.widget.Toast;

公共类MyTask的扩展AsyncTask的<太虚,整型,太虚> {

    上下文语境;
    处理程序处理程序;
    对话对话框;
    TextView的txtprogrss;
    进度进展情况;
    按钮btnCancel;

    MyTask的(上下文的背景下,处理程序处理){
        this.context =背景;
        this.handler =处理程序;

    }

    MyTask的(上下文的背景下){
      this.context =背景;
      this.handler =处理程序;
    }

    @覆盖
    在preExecute保护无效(){

        super.on preExecute();
        //创建对话框
        对话框=新的对话框(背景);
        dialog.setCancelable(真正的);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.pogressdialog);
        txtprogrss =(TextView中)dialog.findViewById(R.id.txtProgress);
        进度=(进度)dialog.findViewById(R.id.progressBar2);
        btnCancel =(按钮)dialog.findViewById(R.id.btnProgress);

        btnCancel.setOnClickListener(新OnClickListener(){

            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根

                MyTask.this.cancel(真正的);
            }
        });


    }


    @覆盖
    保护无效doInBackground(虚空......为arg0){


        的for(int i = 0; I< 100;我++){
            如果(isCancelled()){
            打破;
            }其他{
            Log.e(以背景,当前值;+ I);
            publishProgress(ⅰ);

            尝试 {
                视频下载(500);
            }赶上(InterruptedException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

            }

        }

        返回null;
    }

    @覆盖
    保护无效onProgressUpdate(整数...值){


        super.onProgressUpdate(值);


        progress.setProgress(值[0]);
        txtprogrss.setText(最新进展+值[0] +%);

    }

    @覆盖
    保护无效onPostExecute(无效的结果){
        super.onPostExecute(结果);


        dialog.dismiss();
        Toast.makeText(背景下,已完成,Toast.LENGTH_LONG).show();

    }





}
 

解决方案

你是不是叫在$ P $ dialog.show() pExecute 的方法,你的的AsyncTask

I am trying to do display a ProgressBar.

I am an Android beginner.

When I press the button, the task should be running in the background, but it does not display the ProgressBar.

What is problem? I cannot understand.

Please help me!

MainActivity:

package com.example.shikkok_services;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {


    private Button btnThread,btntask;
    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnThread=(Button)findViewById(R.id.btnStartThread);
        btntask=(Button)findViewById(R.id.btntsk);

        btntask.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


                //start myTast

                new MyTask(MainActivity.this).execute();


            }
        });



    }

}

MyTask:

 package com.example.shikkok_services;

import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MyTask extends AsyncTask<Void, Integer, Void> {

    Context context;
    Handler handler;
    Dialog dialog;
    TextView txtprogrss;
    ProgressBar progress;
    Button btnCancel;

    MyTask(Context context, Handler handler){
        this.context=context;
        this.handler=handler;

    }

    MyTask(Context context){
      this.context=context;
      this.handler=handler;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        // create dialog
        dialog=new Dialog(context);
        dialog.setCancelable(true);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.pogressdialog);
        txtprogrss=(TextView) dialog.findViewById(R.id.txtProgress);
        progress=(ProgressBar)dialog.findViewById(R.id.progressBar2);
        btnCancel=(Button)dialog.findViewById(R.id.btnProgress);

        btnCancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                MyTask.this.cancel(true);
            }
        });


    }


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


        for (int i = 0; i < 100; i++) {
            if(isCancelled()){
            break;
            }else{
            Log.e("In Background","current value;"+ i);
            publishProgress(i);

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            }

        }

        return null;    
    }

    @Override
    protected void onProgressUpdate(Integer... values) {


        super.onProgressUpdate(values);


        progress.setProgress(values[0]);
        txtprogrss.setText("progress update"+ values[0]+"%");

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);


        dialog.dismiss();
        Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();

    }





}

解决方案

You are not calling dialog.show() in onPreExecute method of your AsyncTask.

这篇关于使用的AsyncTask在Android的显示进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆