如果我使用服务android,为什么我的应用程序会关闭?

时间:2016-06-04 23:45:21

标签: java android shutdown

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

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                                                                                     &LT;&LT; 06-05 09:59:25.526 31873-31873 / com.vitaliylevashov.serviceplayer D / AndroidRuntime:关闭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 / AndroidRuntime:FATAL EXCEPTION:main                                                                                    过程: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)                                                                                        at java.lang.reflect.Method.invokeNative(Native Method)                                                                                        在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(本地方法)

1 个答案:

答案 0 :(得分:0)

在Manifest中添加一个服务标签,可能你没有在manifest中添加它

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

请求发布错误日志/ logcat