android - How to update progress bar in Async Task -
i'm building simple backup app android , code reads , stores messages in phone memory csv file,initially displaying indeterminate progress bar,but user of app might impatient (if messages many) wanted add determinate horizontal progress bar show them actual progress,i've been trying sometime can't right,i know i'm supposed use onprogressupdate method in async task class, don't know how implement i'm getting lot of exceptions,so tried doing in different without method , code below
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 various ui elements button contact,msg; progressdialog pb; textview total; static context mcontext; //method fetch , write contacts .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(); (int = 0; < 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 method used sms , save them 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'); (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 count of contacts in 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 progressdialog pb = new progressdialog(mainactivity.this); pb.settitle("please wait ..."); pb.setindeterminate(false); pb.setmessage("backing 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 progress dialog for(int = 0; < total; i++){ publishprogress(i); } return total_c; } @override protected void onpostexecute(string result) { // todo auto-generated method stub super.onpostexecute(result); //hide progress bar , display 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 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 progress bar , display toast pb.dismiss(); toast.maketext(mainactivity.this, result + " succesfully backed up", toast.length_long).show(); } } }
the progress bar shows dosen't update...apart code works flawlessly...please need suggestions on how can implement determinate progess bar code.thanks
edit: i've gone throught 2 answers posted, , i've re-written code, instead of progress bar updating skips 0 100 when it's done, please can point out mistake in code
you want show progress progress dialog ..like integer..loading..45%
if so, link you
Comments
Post a Comment