如何更新异步任务中的进度条

Posted

技术标签:

【中文标题】如何更新异步任务中的进度条【英文标题】:How to update progress bar in Async Task 【发布时间】:2014-11-16 07:46:40 【问题描述】:

我正在为 android 构建一个简单的备份应用程序,此代码读取手机内存中的所有消息并将其存储到一个 csv 文件中,最初我显示的是一个不确定的进度条,但该应用程序的用户可能会不耐烦那个(如果他们的消息太多)所以我想添加一个确定的水平进度条来向他们展示实际的进度,我一直在尝试这样做一段时间,但我做错了,我知道我'我应该在异步任务类中使用 onProgressUpdate 方法,但我真的不知道如何实现它,所以我遇到了很多异常,所以我尝试在没有该方法的情况下以不同的方式执行它,这就是代码如下

package daniel.idea.backup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener 


 public ArrayList<String> smsBuffer = new ArrayList<String>();
 String smsFile = "SMS"+".csv";
 Integer x = 0;
 static Integer Total = 0;


//Initialize the various ui elements
Button contact,msg;
ProgressDialog pb;
TextView total;
static Context mContext;

//Method to fetch and write the contacts to the .vcf file

 public static String getVCF() 
     final String vfile = "Contacts.vcf";
     Cursor phones = mContext.getContentResolver().query(
     ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
     phones.moveToFirst();
     Total = phones.getCount();
     for (int i = 0; i < phones.getCount(); i++) 
         String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
         Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
         AssetFileDescriptor fd;
         try 
             fd = mContext.getContentResolver().openAssetFileDescriptor(uri, "r");
             FileInputStream fis = fd.createInputStream();
             byte[] buf = new byte[(int) fd.getDeclaredLength()];
             fis.read(buf);
             String VCard = new String(buf);
             String path = Environment.getExternalStorageDirectory().toString() + File.separator + vfile;
             FileOutputStream mFileOutputStream = new FileOutputStream(path, true);
             mFileOutputStream.write(VCard.toString().getBytes());
             phones.moveToNext();
             Log.d("Vcard", VCard);
             mFileOutputStream.close();
          catch (Exception e1) 
             // TODO Auto-generated catch block
             e1.printStackTrace();
         
     
    return phones.getCount()+ " Contacts";
 


 //This is the method used to get the sms and save them to a file

        private String  backupSMS()
        smsBuffer.clear();
        Uri mSmsinboxQueryUri = Uri.parse("content://sms");
        Cursor cursor1 = mContext.getContentResolver().query(
                mSmsinboxQueryUri,
                new String[]  "_id", "thread_id", "address", "person", "date",
                        "body", "type" , null, null, null);
        //startManagingCursor(cursor1);
        String[] columns = new String[]  "_id", "thread_id", "address", "person", "date", "body",
                "type" ;
        if (cursor1.getCount() > 0) 
            String count = Integer.toString(cursor1.getCount());
            Log.d("Count",count);
            while (cursor1.moveToNext()) 

                 String messageId = cursor1.getString(cursor1
                        .getColumnIndex(columns[0]));

                 String threadId = cursor1.getString(cursor1
                        .getColumnIndex(columns[1]));

                String address = cursor1.getString(cursor1
                        .getColumnIndex(columns[2]));
                String name = cursor1.getString(cursor1
                        .getColumnIndex(columns[3]));
                String date = cursor1.getString(cursor1
                        .getColumnIndex(columns[4]));
                String msg = cursor1.getString(cursor1
                        .getColumnIndex(columns[5]));
                String type = cursor1.getString(cursor1
                        .getColumnIndex(columns[6]));



                smsBuffer.add(messageId + ","+ threadId+ ","+ address + "," + name + "," + date + " ," + msg + " ,"
                        + type);


                       
            generateCSVFileForSMS(smsBuffer);

        
        return cursor1.getCount() + "Messages";         
    


     private void generateCSVFileForSMS(ArrayList<String> list)
    

        try 
        
            String storage_path = Environment.getExternalStorageDirectory().toString() + File.separator + smsFile;
            FileWriter write = new FileWriter(storage_path);

            write.append("messageId, threadId, Address, Name, Date, msg, type");
            write.append('\n');

            for (String s : list)
            
                write.append(s);
                write.append('\n');
            
            write.flush();
            write.close();
        

        catch (NullPointerException e) 
        
            System.out.println("Nullpointer Exception "+e);
             //  e.printStackTrace();
         
        catch (IOException e) 
        
            e.printStackTrace();
        
        catch (Exception e) 
        
            e.printStackTrace();
       

    


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

    mContext = MainActivity.this;
    Cursor phones_total = mContext.getContentResolver().query(
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    //This gets the count of all the contacts in the phone memory
    Integer total_contacts = phones_total.getCount();

    contact = (Button) findViewById (R.id.bt_contacts);
    msg = (Button) findViewById (R.id.bt_messages);
    total = (TextView) findViewById (R.id.tv_total);


    contact.setOnClickListener(this);
    msg.setOnClickListener(this);
    total.setText(total_contacts +" Contacts");


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

    switch(v.getId())

    case R.id.bt_contacts:

        MyTask task = new MyTask();
        task.execute("Param 1","Param2","Param3");


        break;

    case R.id.bt_messages:

         SmsTask task2 = new SmsTask();
         task2.execute();
    




public class MyTask extends AsyncTask<String, Integer, String>

    @Override
    protected void onPreExecute() 

        //make the progressDialog
        pb = new ProgressDialog(MainActivity.this);
        pb.setTitle("Please wait ...");
        pb.setIndeterminate(false);
        pb.setMessage("Backing up contacts...");
        pb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pb.setProgress(0);
        pb.setMax(100);
        pb.show();


    

    @Override
    protected String doInBackground(String... arg0) 
        // TODO Auto-generated method stub
        mContext = MainActivity.this;

        String total_c = getVCF();

         //Increment the progress dialog

        for(int i = 0; i < Total; i++)

            publishProgress(i);

        

        return total_c;

    

    @Override
    protected void onPostExecute(String result) 
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        //hide the progress bar and display a toast message

        pb.dismiss();
        Toast.makeText(MainActivity.this, result + " Succesfully backed up", Toast.LENGTH_LONG).show();


    

    @Override
    protected void onProgressUpdate(Integer... values) 
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);

        pb.incrementProgressBy(values[0]);

        values[0]++;
    





public class SmsTask extends AsyncTask <String,Integer,String>


        protected void onPreExecute() 

            //show the progress bar
            pb = ProgressDialog.show(MainActivity.this, "Processing",
                    "please wait", true);

            


        @Override
        protected String doInBackground(String... params) 
            // TODO Auto-generated method stub
            mContext = MainActivity.this;

            String total_sms = backupSMS();

            return total_sms;



        


        @Override
        protected void onPostExecute(String result) 
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            //hide the progress bar and display a toast

            pb.dismiss();
            Toast.makeText(MainActivity.this, result + " Succesfully backed up", Toast.LENGTH_LONG).show();



    








进度条显示,但没有更新...除此之外,代码完美无缺...请我需要有关如何在此代码中实现确定进度条的建议。谢谢

编辑:我已经完成了发布的两个答案,并且我已经重新编写了我的代码,而不是更新进度条,它只是在完成后从 0 跳到 100,请有人可以指出我在这段代码中的错误

【问题讨论】:

嘿,我知道帖子很旧,但我想出了一个(不太好)的解决方案,以防你仍然需要它或其他任何人,我只需在AsyncTask 中使用sleep(1)。这允许主线程执行(更新进度条),因为它被AsyncTask 线程阻塞。虽然这会影响性能,但我还没有找到更好的方法,这里有更多信息 my question 【参考方案1】:

我没有看到您在代码中的任何位置更新进度条。也许是因为它周围有太多不相关的代码......

但这是你必须做的:

AsyncTask 无法通过其方法 doInBackground() 访问 UI。在执行 AsyncTask 期间,您只能从 onProgressUpdate() 函数访问 UI。您没有定义此函数,因此无法执行任何更新。

文档:onProgressUpdate()

【讨论】:

我想我们也可以从onPreExecute()onPostExecute() 访问用户界面。 :) 啊等等,我弄错了。我只是在考虑进度更新...:D【参考方案2】:

你想用你的进度对话框显示进度..就像一个整数..正在加载..45%

如果是这样,此链接将对您有所帮助

android how to work with asynctasks progressdialog

【讨论】:

【参考方案3】:

这是一个旧帖子,但我想我会插话,看看我是否可以帮助解决它。您没有获得更新进度的原因是您必须在doInBackground() 内的主while() 循环内调用publishProgress()。其他答案中链接的资源应详细说明如何使用publishProgress() 命令!

【讨论】:

以上是关于如何更新异步任务中的进度条的主要内容,如果未能解决你的问题,请参考以下文章

如何在异步任务中更新进度栏

android:异步任务asyncTask介绍及异步任务下载图片(带进度条)

我如何在我的异步任务类中使用圆形旋转进度条

当片段视图加载是异步任务的一部分时,如何在片段加载之前显示进度条?

尝试连接到服务器时带有异步任务的进度条

python异步任务