android - Notification is slowing down -


in application download files using intent service. while download starts download progress notification created. works fine, problem notification slows down phone: following code: note: have removed non related part of code make simple , clear:

public void startdownload(string name, string packagename, string path, boolean obb, boolean data) {     try {         notificationid = generatenoficationid(packagename);         intent = new intent(getapplicationcontext(), downloadlistview.class);         pendingintent pi = pendingintent.getactivity(getapplicationcontext(), 0, i, 0);              mbuilder = new notificationcompat.builder(this);             mbuilder.setcontenttitle(name)                     .setcontenttext("downloading...")                     .setsmallicon(r.drawable.newiconpurple)                     .setcontentinfo("0%")                     .setprogress(100, 0, true)                     .setcontentintent(pi)                     .setongoing(true)                     .setautocancel(true);              nm = (notificationmanager) getsystemservice(notification_service);             notification = mbuilder.build();             startforeground(notificationid, notification);              url = new url(ipclass.serverip + path + "/" + packagename);             connection = (httpurlconnection) url.openconnection();             connection.setdoinput(true);             connection.setdooutput(true);             connection.setreadtimeout(7000);             long filelength = connection.getcontentlength();             connection.connect();             input = connection.getinputstream();             output = new fileoutputstream("/sdcard/downloadtmp/" + packagename, true);             byte datasize[] = new byte[16384];             int count;             downloadedsofar = 0;             continuedownload = true;             while ((count = input.read(datasize)) > 0 && continuedownload) {                 if (!downloadcancellist.contains(packagename)) {                     downloadedsofar = downloadedsofar + count;                     output.write(datasize, 0, count);                     progress = (int) ((downloadedsofar * 100l) / filelength);                     progresschange(name, progress, packagename, obb, data);                 } else {                     continuedownload = false;                     int startid = downloadidlist.get(packagename);                     stopself(startid);                     nm.cancel(notificationid);                     downloadcancellist.remove(packagename);                     stopforeground(true);                 }                 thread.sleep(250);             }         }     } catch (exception ex) {         nm.cancel(notificationid);     } {         try {             if (output != null) output.close();             if (input != null) input.close();         } catch (ioexception ignored) {          }         if (connection != null) {             connection.disconnect();         } else if (resumeconnection != null) {             resumeconnection.disconnect();         }     } } 

and following progresschange method:

   void progresschange(final string name, final int progress, final string packagename, boolean obb, boolean data) {          if (lastupdate != progress) {             lastupdate = progress;             if (progress < 100) {                  mbuilder.setprogress(100, integer.valueof(progress), false).setcontentinfo(progress + "%");                 nm.notify(notificationid, mbuilder.build());              } else if (progress == 100) {                 stopforeground(true);                 mbuilder.setcontenttext("download finished").setprogress(0, 0, false).setongoing(false).setcontentinfo("");                 nm.notify(notificationid, mbuilder.build());                 new downloadcontroller().removefromdownloadlist(packagename);                 new downloadfinished(name, packagename, obb, data).execute();             }         }     } 

please me!

it's bad idea send notification many times. each notification needs inflated, stored , displayed. inflated notifications aren't removed memory instantly when removed screen. you'r creating huge stack of views occupies ram.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -