我正在尝试制作一个简单的音频播放器。当我在没有服务的主活动中创建它时,它可以正常工作,正在播放音乐(如果按电源按钮以关闭屏幕,则它可以工作),并且在打开屏幕后仍然可以正常工作。但是,如果我使用服务在后台播放音乐,则当我按下电源按钮打开屏幕时,我的应用程序会关闭。

public class MainActivity extends AppCompatActivity {

    static ArrayList<HashMap<String, Object>> listSongs = new ArrayList<>();
    Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listSongs = getAllMusicInfo();
        intent = new Intent(this, BackgroundPlayer.class);

        Button buttonStart = (Button) findViewById(R.id.startService);
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startService(intent);
                Toast.makeText(MainActivity.this, "It's started", Toast.LENGTH_SHORT).show();
            }
        });

        Button buttonStop = (Button) findViewById(R.id.StopService);
        buttonStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
                Toast.makeText(MainActivity.this, "It's stopped", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class BackgroundPlayer extends Service implements MediaPlayer.OnCompletionListener {
    MediaPlayer player;

    @Override
    public void onCreate() {
        super.onCreate();

        player = new MediaPlayer();

        try {
            player.setDataSource((String) MainActivity.listSongs.get(0).get("path"));
            player.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return START_STICKY;
    }

    @Override
    public void onCompletion(MediaPlayer mp) {}
}

06-05 09:58:54.206 31873-31873/com.vitaliylevashov.serviceplayer V/MediaPlayer: start


06-05 09:58:54.206 31873-31885 / com.vitaliylevashov.serviceplayer V / MediaPlayer:收到消息msg = 6,ext1 = 0,ext2 = 0
06-05 09:58:54.206 31873-31885 / com.vitaliylevashov.serviceplayer V / MediaPlayer:已收到MEDIA_STARTED
06-05 09:58:54.206 31873-31885 / com.vitaliylevashov.serviceplayer V / MediaPlayer:回调应用程序
06-05 09:58:54.206 31873-31885 / com.vitaliylevashov.serviceplayer V / MediaPlayer:从回调返回
06-05 09:58:54.236 31873-31873 / com.vitaliylevashov.serviceplayer I / MediaPlayer:不要发送意图。 msg.arg1 = 0,msg.arg2 = 0
06-05 09:58:54.236 31873-31873 / com.vitaliylevashov.serviceplayer E / MediaPlayer:应该已经设置了字幕控制器
06-05 09:58:54.256 31873-31873 / com.vitaliylevashov.serviceplayer V / MediaPlayer-JNI:getCurrentPosition:192(msec)
06-05 09:58:54.256 31873-31873 / com.vitaliylevashov.serviceplayer V / MediaPlayer-JNI:isPlaying:1
                                                                                    <<
06-05 09:59:25.526 31873-31873 / com.vitaliylevashov.serviceplayer D / Android运行时:关闭VM
06-05 09:59:25.526 31873-31873 / com.vitaliylevashov.serviceplayer W / dalvikvm:threadid = 1:线程以未捕获的异常退出(group = 0x41660bc0)
06-05 09:59:25.526 31873-31873 / com.vitaliylevashov.serviceplayer E / Android运行时:致命异常:主要
                                                                                   进程:com.vitaliylevashov.serviceplayer,PID:31873
                                                                                   android.database.StaleDataException:尝试在关闭游标后访问游标。
                                                                                       在android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:64)
                                                                                       在android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:133)
                                                                                       在android.database.CursorWrapper.requery(CursorWrapper.java:186)
                                                                                       在android.app.Activity.performRestart(Activity.java:5346)
                                                                                       在android.app.ActivityThread.handleSleeping(ActivityThread.java:3487)
                                                                                       在android.app.ActivityThread.access $ 3000(ActivityThread.java:155)
                                                                                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1428)
                                                                                       在android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       在android.os.Looper.loop(Looper.java:136)
                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5433)
                                                                                       在java.lang.reflect.Method.invokeNative(本机方法)
                                                                                       在java.lang.reflect.Method.invoke(Method.java:515)
                                                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                                       在dalvik.system.NativeStart.main(本机方法)

最佳答案

在清单中添加服务标签,也许您还没有在清单中添加服务标签

<service android:name=".MyService" />

请求发布错误日志/ logcat

07-24 09:37