my android application freezes few seconds when try send database file remote server, there anyway can set background thread using multithreading or such feature?
this code dialog box appears ask me if want send
public void showyesnobox(){ dialoginterface.onclicklistener dialogclicklistener = new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { switch (which){ case dialoginterface.button_positive: //yes button clicked senddata(); finish();//go previous activity overridependingtransition(r.anim.fadein, r.anim.fadeout); break; case dialoginterface.button_negative: //no button clicked finish();//go previous activity overridependingtransition(r.anim.fadein, r.anim.fadeout); break; } } }; alertdialog.builder builder = new alertdialog.builder(this, alertdialog.theme_holo_dark); builder.setmessage("do want send data now?"); builder.setpositivebutton("yes", dialogclicklistener); builder.setnegativebutton("no", dialogclicklistener); builder.show(); } and here code executes when click "yes" button
public void senddata() { file path = getdatabasepath(databasehelper.database_name); if (path.exists()) copydatabase(path); } /** * copy database sdcard * @param file */ private void copydatabase(file file) { simpledateformat dateformat1 = new simpledateformat("ddmmyy-hhmmss"); string datestring = dateformat1.format(new date()); string pathdest = getdir().getabsolutepath() + configuration.lost_folder + "/database/"; string pathdir = pathdest; file dir = new file(pathdir); if (!dir.exists()) dir.mkdirs(); string namefile = file.getname(); int pos = namefile.lastindexof('.'); if (pos != -1) { string ext = namefile.substring(pos + 1); string name = namefile.substring(0, pos - 1); pathdest += name; pathdest += "_" + datestring; pathdest += ext; } else { pathdest += namefile; pathdest += "_" + datestring; } file filedest = new file(pathdest); try { if (filedest.createnewfile()) copyfile(file.getabsolutepath(), pathdest); } catch (ioexception e) { e.printstacktrace(); } } any problem appreciated while problem doesn't prevent user being able send data anoying stuck on 1 screen few seconds while attempts execute action.
while problem doesn't prevent user being able send data anoying stuck on 1 screen few seconds while attempts execute action.
you can see asynctask. same purposes. user need not stuck on ui when want long running processes including taking server, doing database stuff etc. can done in background using asynctask. there many tutorials out there. google it. checkout android background processing threads, handlers , asynctask - tutorial , what arguments passed asynctask<arg1, arg2, arg3>? . hope helps.
Comments
Post a Comment