An SO User
An SO User

Reputation: 24998

IntentService does not start

My IntentService looks like this:

public class MusicService extends IntentService{

    final static String NAME = "MusicService";

    //------------------------------------------------------------------------------
    public MusicService(String name) {
        super(NAME);
    }
    //------------------------------------------------------------------------------
    public MusicService() {
        super(NAME);
    }
    //------------------------------------------------------------------------------
    @Override
    protected void onHandleIntent(Intent intent) {
        // Toast never comes up
        Toast.makeText(getApplicationContext(), NAME, Toast.LENGTH_SHORT).show();
        PodcastrApplication.newInstance().getMediaPlayer().start();
    }
    //------------------------------------------------------------------------------
}

I have a MediaPlayer that I keep in my Application to reduce object instantiations. The idea is that once the app goes out of focus, the media player will play. However, the service is never fired.

Here is how I am calling the service from the onStop() of my Fragment

showMediaPlayerNotification();
Intent music = new Intent(getActivity(), MusicService.class);
getActivity().startService(music);  

I have it declared in the manifest also. There is no error in logcat, either.

What is going on?

Update 1:

Once you call startService(), the IntentService does the work defined in its onHandleIntent() method, and then stops itself.

Is it that the service is stopping itself because it is just one line of execution?
Should I move the media player to a thread?

Update 2:

<service
            android:name=".service.MusicService"
            android:enabled="true"
            android:exported="false" />

Upvotes: 1

Views: 1696

Answers (1)

frogatto
frogatto

Reputation: 29285

You've called Toast.makeText() in a background thread!, Any UI stuff must be done in UI thread.

Note that: IntentService uses a background thread but Service uses UI thread

You can try:

public class MusicService extends IntentService{

    Handler mHandler;
    final static String NAME = "MusicService";

    //------------------------------------------------------------------------------
    public MusicService(String name) {
        super(NAME);
        mHandler = new Handler();
    }
    //------------------------------------------------------------------------------
    public MusicService() {
        super(NAME);
        mHandler = new Handler();
    }
    //------------------------------------------------------------------------------
    @Override
    protected void onHandleIntent(Intent intent) {
        mHandler.post(new Runnable{

            @Override
            public void run(){
                Toast.makeText(getApplicationContext(), NAME, Toast.LENGTH_SHORT).show();
            }            

        });
        // Toast never comes up

        PodcastrApplication.newInstance().getMediaPlayer().start();
    }
    //------------------------------------------------------------------------------
}

Upvotes: 1

Related Questions