Android 为什么在应用程序进程终止后重新启动服务

Android 为什么在应用程序进程终止后重新启动服务,android,service,Android,Service,我有以下启动服务的代码: package com.example.serviceapp; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extend

我有以下启动服务的代码:

package com.example.serviceapp;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {
    private static final String TAG = "MyService";
    MediaPlayer player;

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

    @Override
    public void onCreate() {
        Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onCreate");

        player = MediaPlayer.create(this, R.raw.music);
        player.setLooping(false); // Set looping
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onStart");
        player.start();

        /*
         * START_STICKY: Constant to return from onStartCommand(Intent, int,
         * int): if this service's process is killed while it is started (after
         * returning from onStartCommand(Intent, int, int)), then leave it in
         * the started state but don't retain this delivered intent.
         */
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
        Log.d(TAG, "onDestroy");
        player.stop();
    }

}
但是当应用程序进程被终止时,服务会停止并重新启动,为什么

这是我启动/停止服务的方式:

public class MainActivity extends Activity {
    private static final String TAG = "ServicesDemo";
    Button buttonStart, buttonStop;

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

        buttonStart = (Button) findViewById(R.id.buttonStart);
        buttonStop = (Button) findViewById(R.id.buttonStop);

        buttonStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: starting srvice");
                startService(new Intent(getApplicationContext(),
                        MyService.class));
            }
        });
        buttonStop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: stopping srvice");
                stopService(new Intent(getApplicationContext(), MyService.class));
            }
        });
    }

}

启动服务时,您正在使用
START\u
标志。这意味着必须显式启动和停止服务。如果服务在这段时间内被终止,android会重新启动它。这可以确保,除非您发送停止服务的意图,否则它将继续运行


您可以设置标志
START\u NOT\u STICKY
,该标志将告诉android在服务被终止时不要自动重新启动服务。

如何启动服务,如何在MainActivity中实例化服务?