android - Activity doesn't work after starting background download service -
i have developed android app loads data list view database. , there function download audio internet.
i've created service download audio , bind activity. when start download background service , quit activity , restart same activity data doesn't load database.
activity doesn't crash or hangs showing loading dialog.
this happens while download running.
here download service code:
public class downloadservice extends service { string path,url,filename,folderpath=null; asynctask<string, string, string> task=null; listenquranactivity lq=new listenquranactivity(); private final ibinder dbind = new dbinder(); notificationcompat.builder builder; notification not; notificationmanager mnm ; public boolean running=false; //binder public class dbinder extends binder { public downloadservice getservice() { return downloadservice.this; } } //activity bind service @override public ibinder onbind(intent intent) { return dbind; } //release resources when unbind @override public boolean onunbind(intent intent){ return false; } public void oncreate(){ //create service super.oncreate(); mnm = (notificationmanager)getsystemservice(notification_service); } public void go(){ if(!running){ task=new download().execute(url); running=true; } else{ toast.maketext(getapplicationcontext(), "try after download complete", toast.length_short).show();} } public void setpath(string newpath,string name){ path=newpath; filename=name; } public void seturl(string newurl){ url=newurl; } public void setdest(string dest){ folderpath=dest; } public void stop(){ task.cancel(true); toast.maketext(getapplicationcontext(), "download cancelled", toast.length_short).show(); } public void zipex(){ inputstream is; zipinputstream zis; try { string fname; = new fileinputstream(path); zis = new zipinputstream(new bufferedinputstream(is)); zipentry ze; byte[] buffer = new byte[1024]; int count; while ((ze = zis.getnextentry()) != null) { // zapis souboru fname = ze.getname(); // need create directories if not exists, or // generate exception... if (ze.isdirectory()) { file fmd = new file(fname); fmd.mkdirs(); continue; } fileoutputstream fout = new fileoutputstream(folderpath+"/"+fname); // cteni zipu zapis while ((count = zis.read(buffer)) != -1) { fout.write(buffer, 0, count); } fout.close(); zis.closeentry(); } zis.close(); file file=new file(path); if(file.exists())file.delete(); } catch(ioexception e) { e.printstacktrace(); } } class download extends asynctask<string, string, string> { /** * before starting background thread * show progress bar dialog * */ @suppresslint("newapi") @override protected void onpreexecute() { super.onpreexecute(); intent notintent = new intent(downloadservice.this,prompt.class); notintent.addflags(intent.flag_activity_exclude_from_recents|intent.flag_from_background); pendingintent pendint = pendingintent.getactivity(getapplicationcontext(), 0,notintent, intent.flag_activity_brought_to_front); builder = new notificationcompat.builder(getapplicationcontext()); builder.setcontentintent(pendint) .setsmallicon(r.drawable.ic_launcher) .setticker("downloading "+filename) .setongoing(true) .setcontenttitle(filename) .setprogress(100, 0, true); not = builder.build(); startforeground(url.hashcode(), not); } /** * downloading file in background thread * */ @override protected string doinbackground(final string... f_url) { int count; try { url url = new url(f_url[0]); urlconnection conection = url.openconnection(); conection.connect(); conection.setconnecttimeout(60000); // getting file length int lenghtoffile = conection.getcontentlength(); // input stream read file - 8k buffer inputstream input = new bufferedinputstream(url.openstream(), 8192); // output stream write file outputstream output = new fileoutputstream(path); byte data[] = new byte[1024]; long total = 0; while ((count = input.read(data)) != -1) { if(task.iscancelled()){ running=false; lq.deletemyfile(path); stopforeground(true); stopself(); break; } total += count; // publishing progress.... // after onprogressupdate called publishprogress(""+(int)((total*100)/lenghtoffile)); builder.setcontenttext(lq.setpro(total, lenghtoffile)); // writing data file output.write(data, 0, count); } // flushing output output.flush(); // closing streams output.close(); input.close(); } catch (exception e) { e.printstacktrace(); } return null; } /** * updating progress bar * */ @suppresslint("newapi") protected void onprogressupdate(string... progress) { // setting progress percentage builder.setprogress(100, integer.parseint(progress[0]), false); not=builder.build(); mnm.notify(url.hashcode(), not); } /** * after completing background task * dismiss progress dialog * **/ @override protected void onpostexecute(string file_url) { // check zip file if(folderpath!=null){ zipex(); } toast.maketext(getapplicationcontext(), "download complete", toast.length_short).show(); running=false; stopforeground(true); intent notintent = new intent(downloadservice.this,mainactivity.class); notintent.addflags(intent.flag_activity_exclude_from_recents|intent.flag_from_background); pendingintent pendint = pendingintent.getactivity(getapplicationcontext(), 0,notintent, intent.flag_activity_brought_to_front); notificationcompat.builder xbuilder = new notificationcompat.builder(downloadservice.this); xbuilder .setcontentintent(pendint) .setsmallicon(r.drawable.ic_launcher) .setticker(filename +" download completed") .setcontenttitle(filename+" downloaded"); notification xnot = xbuilder.build(); mnm.notify(url.hashcode(),xnot); stopself(); } } public void ondestroy() { stopforeground(true); stopself(); }
}
and here activity connection
//connect service private serviceconnection downloadconnection = new serviceconnection(){ @override public void onserviceconnected(componentname name, ibinder service) { dbinder binder = (dbinder)service; //get service dsrv = binder.getservice(); dbound = true; } @override public void onservicedisconnected(componentname name) { dbound = false; } }; //start , bind service when activity starts @override protected void onstart() { super.onstart(); if(dintent==null){ dintent = new intent(this, downloadservice.class); bindservice(dintent, downloadconnection, context.bind_auto_create); startservice(dintent); } }
there no other error in activity except this. may there problem in download service.
please me. thanks.
your service not background service, bound service since binding , when quit activity, quit service... that's bound service supposed do.
you need first call startservice , after bind it, background service, calling bind before start making service bound one
//start , bind service when activity starts
@override protected void onstart() { super.onstart(); if(dintent==null){ dintent = new intent(this, downloadservice.class); startservice(dintent); bindservice(dintent, downloadconnection, context.bind_auto_create); } }
Comments
Post a Comment