android - How to show ProgressDialog in Service -


i have written app streaming rtsp link.i have stream url in custom service class.i want show proggresdialog while url loading in other words before start music.here codes;

public class mymediaplayerservice extends service implements oncompletionlistener{  private string path = "rtsp://someurl"; mediaplayer mediaplayer; private progressdialog pd;  @override public ibinder onbind(intent intent) {     return null; }  @override public void oncreate() {     super.oncreate();     mediaplayer = new mediaplayer();     mediaplayer.setoncompletionlistener(this); }  public void surfacechanged(surfaceholder surfaceholder, int i, int j, int k) { }  public void surfacedestroyed(surfaceholder surfaceholder) { }  public void surfacecreated(surfaceholder holder) { }  public void oncompletion(mediaplayer _mediaplayer) {     stopself(); }  @override public int onstartcommand(intent intent, int flags, int startid) {      if (!mediaplayer.isplaying()) {         pd = new progressdialog(getapplicationcontext());         pd.setmessage("loading...");         pd.setcancelable(false);         pd.show();         mediaplayer.setaudiostreamtype(audiomanager.stream_music);         try {             mediaplayer.reset();             mediaplayer.setdatasource(path);             mediaplayer.prepareasync();         } catch (exception e) {             e.printstacktrace();         }          mediaplayer                 .setonpreparedlistener(new mediaplayer.onpreparedlistener() {                     @override                     public void onprepared(mediaplayer mp) {                         pd.dismiss();                         mp.start();                     }                 });     }      return start_not_sticky; }    public void ondestroy() {     if (mediaplayer.isplaying()) {         mediaplayer.stop();      }     mediaplayer.release(); } 

}

an getting errors on log;

08-15 22:42:15.384: e/androidruntime(1090): fatal exception: main 08-15 22:42:15.384: e/androidruntime(1090): java.lang.runtimeexception: unable start     service com.applogist.servis.mymediaplayerservice@42417340 intent { cmp=com.applogist.standartfm/com.applogist.servis.mymediaplayerservice }:    android.view.windowmanager$badtokenexception: unable add window --  token null not application 

how show proggres dialog now?

it advised using notification service|sending notifications user official developer guide.

also, if need play music in service, need in thread.

caution: service runs in main thread of hosting process—the service not create own thread , not run in separate process (unless specify otherwise). means that, if service going cpu intensive work or blocking operations (such mp3 playback or networking), should create new thread within service work. using separate thread, reduce risk of application not responding (anr) errors , application's main thread can remain dedicated user interaction activities.

however, if insist show dialog , music played after downloaded. think can use dialog class has inner asynctask class. can downloading task in asynctask's doinbackground (params... params) method , update ui of dialog @override protected void onprogressupdate (progress... values) of asynctask.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -