android - Making AsyncTask Static (Having Issues, code provided) -


i need know how start asynctask in original activity service that activity starts. fetches music , plays in service , want grab next song after has completed right here:

mediaplayer.setoncompletionlistener(new mediaplayer.oncompletionlistener()      {                    public void oncompletion(mediaplayer mp)          {                            if (mainmenu.radioflag == 1) {                 // start new asynctask             } else {             stopself();             }         }                }); 

down below asynctask, in radio.class. not know how make static references radio.this , getting errors on startservice(i) , stopservice(i). appreciate help, thanks.

private class loadlist extends asynctask<string, userrecord, jsonarray> {      string result = null;     inputstream = null;     stringbuilder sb = null;     jsonarray jarray;      @override     protected void oncancelled() {         toast toast = toast.maketext(getapplicationcontext(), "loading failed, trying again", toast.length_long);         toast.show();         new loadlist().execute(linkurl);     }      protected jsonarray doinbackground(string... link) {          linkurl = link[0];         uri uri = null;         try {             uri = new uri(link[0]);         } catch (urisyntaxexception e2) {             // todo auto-generated catch block             e2.printstacktrace();         }          //http post         try{             httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost(uri);             //httppost.setentity(new urlencodedformentity(namevaluepairs));             httpresponse response = httpclient.execute(httppost);             httpentity entity = response.getentity();             = entity.getcontent();         }catch(exception e){             log.e("log_tag", "error in http connection"+e.tostring());         }          //convert response string         try{             bufferedreader reader = new bufferedreader(new inputstreamreader(is,"utf8"),8);             sb = new stringbuilder();             sb.append(reader.readline() + "\n");              string line="0";             while ((line = reader.readline()) != null) {                 sb.append(line + "\n");             }             is.close();             result=sb.tostring();         }catch(exception e){             log.e("log_tag", "error converting result "+e.tostring());         }          if (result == null) {             linkurl = link[0];             cancel(true);                            } else {          try{             jarray = new jsonarray(result);             jsonobject json_data=null;              displayname = new string[jarray.length()];             song_name = new string[jarray.length()];             artist = new string[jarray.length()];             description = new string[jarray.length()];             genre = new string[jarray.length()];             custom_genre = new string[jarray.length()];             album = new string[jarray.length()];             timestamp = new string[jarray.length()];             song_id  = new string[jarray.length()];             avatar = new string[jarray.length()];             test_rating = new float[jarray.length()];             songurl = new string[jarray.length()];             viewss = new string[jarray.length()];             section = new string[jarray.length()];             plays = new string[jarray.length()];             downloads = new string[jarray.length()];             ratingcount = new string[jarray.length()];             commentcount = new string[jarray.length()];             dlflag = new int[jarray.length()];             link1 = new string[jarray.length()];             link2 = new string[jarray.length()];             link3 = new string[jarray.length()];             link4 = new string[jarray.length()];             link5 = new string[jarray.length()];              for(int i=0;i<jarray.length();i++){                 json_data = jarray.getjsonobject(i);                 song_id[i]=json_data.getstring("id");                 song_name[i]=json_data.getstring("songname");                 artist[i]=json_data.getstring("artist");                 displayname[i]=json_data.getstring("displayname");                 description[i]=json_data.getstring("description");                 genre[i]=json_data.getstring("genre");                 custom_genre[i]=json_data.getstring("customgenre");                 album[i]=json_data.getstring("album");                 timestamp[i]=json_data.getstring("format");                 avatar[i]=json_data.getstring("image_url");                 songurl[i]=json_data.getstring("song_url");                 test_rating[i] = (float) json_data.getdouble("rating");                 viewss[i] = json_data.getstring("views");                 section[i] = json_data.getstring("section");                 plays[i] = json_data.getstring("plays");                 downloads[i] = json_data.getstring("downloads");                 ratingcount[i] = json_data.getstring("rating_count");                 commentcount[i] = json_data.getstring("comments");                 dlflag[i] = json_data.getint("downloadflag");                 link1[i] = json_data.getstring("link1");                 link2[i] = json_data.getstring("link2");                 link3[i] = json_data.getstring("link3");                 link4[i] = json_data.getstring("link4");                 link5[i] = json_data.getstring("link5");                             }         }         catch(jsonexception e1){             e1.printstacktrace();          } catch (parseexception e1) {             e1.printstacktrace();         }                    }         return null;     }      protected void onprogressupdate(userrecord... progress) {      }      protected void onpostexecute(jsonarray jarray) {                                                                                     i.putextra("songurl", songurl[0]);         i.putextra("songname", displayname[0]);          mainmenu.radioflag = 1;          ratingbar.setrating(0);          tvtrack.settext(displayname[0]);          if(dialog.isshowing()) {             dialog.dismiss();             }         if(mainmenu.servicerunning == 1) {             stopservice(i);              startservice(i);         } else {             startservice(i);         }          tvtime.settext(timestamp[0]);         tvviews.settext(viewss[0]);         tvcomments.settext(commentcount[0]);         tvratings.settext(ratingcount[0]);         tvplays.settext(plays[0]);         tvdownloads.settext(downloads[0]);          if(description[0] == "null" || description[0] == ""){                                                                            viewdesc.setvisibility(view.gone);             viewdesc2.setvisibility(view.gone);             descriptiontitle.setvisibility(view.gone);             tvdescription.setvisibility(view.gone);         } else {             viewdesc.setvisibility(view.visible);                    descriptiontitle.setvisibility(view.visible);             tvdescription.setvisibility(view.visible);             tvdescription.settext(description[0]);             viewdesc2.setvisibility(view.visible);         }                   }                                         protected void onpreexecute(){                 dialog = progressdialog.show(radio.this, "",                      "loading. please wait...", true);                                        } } 

you don't want make asynctask static if could. each instance 1 shot , can never run again.

it seems have fair bit of communication between activity , service. in such situation, should binding service activity. you'll have easy channel calling methods , forth, or can set message , handler channels if want things asynchronously.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -