Android studio change async task to a handler -


i need change application uses handler rather asynctask run fake download. i've been stuck on while. have tried using samples figure out think notification thread confuses me. doing head in.

public class responsive extends appcompatactivity {  // 5. declare , populate string array listview items (put directly underneath class declaration) private static final string[] list_items = new string[]{"one", "two", "three", "four"};  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_responsive);      // 4. call setupbutton() , setuplistview() oncreate method.     setupbutton();     setuplistview(); }  @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_responsive, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); }  // 3. open responsive.java editing , add following 3 methods (don’t rid of of existing code):  private void setuplistview() {     listview lv = (listview) findviewbyid(r.id.listview);     lv.setadapter(new arrayadapter<string>(this, android.r.layout.simple_list_item_1, list_items)); }  private void setupbutton() {     button button = (button) findviewbyid(r.id.notificationbutton);     button.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {             createnotification();         }     }); }  private void createnotification() {     new downloader(getapplicationcontext()).execute(0); } } 

public class notificationhelper {      private context mcontext;     private int notification_id = 1;     private notificationcompat.builder mnotification;     public notificationmanager mnotificationmanager;     private pendingintent mcontentintent;     private charsequence mcontenttitle;     public notificationhelper(context context)     {         mcontext = context;     }      /**      * put notification status bar      */     public void createnotification() { //get notification manager         mnotificationmanager = (notificationmanager) mcontext.getsystemservice(context.notification_service);         mcontenttitle = mcontext.getstring(r.string.content_title); //create notification         mnotification = new notificationcompat.builder(mcontext)                 .setsmallicon(r.drawable.ic_action_download)                 .setcontenttitle(mcontenttitle)                 .setticker(mcontext.getstring(r.string.download_ticker))                 .setongoing(true);  //text of notification in notification drawer         charsequence contenttext = "0% complete";         mnotification.setcontenttext(contenttext); //we not going use pending intent,so create blank 1         intent notificationintent = new intent();         mcontentintent = pendingintent.getactivity(mcontext, 0, notificationintent, 0);         mnotification.setcontentintent(mcontentintent); //show notification         mnotificationmanager.notify(notification_id, mnotification.build());     }      /**      * receives progress updates background task , updates *the notification bar      * @param percentagecomplete      */     public void progressupdate(int percentagecomplete) {         //build new status message         charsequence contenttext = percentagecomplete + "% complete";         //publish notification         mnotification.setcontenttext(contenttext);         mnotificationmanager.notify(notification_id, mnotification.build());     }      /**      * called when background task complete, removes *notification.      */     public void completed(){         //remove notification status bar         mnotificationmanager.cancel(notification_id);     }  } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -