android - IntentService 没有启动

标签 android android-mediaplayer intentservice

我的 IntentService 看起来像这样:

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();
    }
    //------------------------------------------------------------------------------
}

我有一个 MediaPlayer,我将它保存在我的 Application 中以减少对象实例化。这个想法是,一旦应用程序失去焦点,媒体播放器就会播放。但是,该服务永远不会被触发。

下面是我从 FragmentonStop() 调用服务的方式

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

我也在 list 中声明了它。 logcat也没有报错。

这是怎么回事?

更新 1:

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

是不是服务自己停止了,因为它只是执行了一行?
我应该将媒体播放器移动到线程吗?

更新 2:

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

最佳答案

您已经在后台线程中调用了 Toast.makeText()!任何 UI 操作都必须在 UI 线程中完成。

请注意:IntentService 使用后台线程,但 Service 使用 UI 线程

你可以试试:

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();
    }
    //------------------------------------------------------------------------------
}

关于android - IntentService 没有启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25472615/

相关文章:

android - 如何在 Android 中获取 HTTP 请求/响应时间

Android - 将 rtsp (h264) 流保存到 mp4 文件

android - 如果应用程序在前台,则替代操作的设计模式?

android - IntentService + startForeground 与 JobIntentService

android - 如何停止 android Background IntentService?

Android RadioButton 方向

Android:带进度条的 EditText?

java - 如何在我的 GridView 中包含 SearchView?

java - 如何解决 MediaPlayer 错误 "Can' t 播放此视频”

java - 暂停其他媒体应用