应用程序终止时服务重新启动

Service restarted when Application Killed

我已经阅读了这个 SO 问题,但它对我没有帮助...Android Background Service is restarting when application is killed。但是这里提到的答案根本没有帮助我。请考虑这个

我想为背景音乐创建一个服务,并且即使我的应用程序已关闭也想播放它。所以我写了下面的代码...

package elsner.com.mediaplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by nteam on 6/2/16.
 */
public class BackGround extends Service {

    MediaPlayer mPlayer;


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


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

        Log.i("niral", "Service Create");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("niral", "Service Start");
        final Handler h = new Handler();
        final long delayInMilliseconds = (6000*10);

        new Thread(new Runnable() {

            @Override
            public void run() {
                mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.aayat);
                mPlayer.start();

                h.postDelayed(new Runnable() {
                    public void run() {

                        mPlayer.stop();
                    }
                }, delayInMilliseconds);
            }
        }).start();



        return START_NOT_STICKY;
    }


}

一切正常,直到我从后台终止应用程序。一旦我从后台终止应用程序,我的服务就会重新启动,音乐文件将从顶部播放。

我通过在最近使用的应用程序中刷掉应用程序来终止应用程序。

请帮助我。

这是我在这里的回答 post 的重新 。为了方便我重新post

应用程序和服务存在于同一个进程中,这意味着当应用程序被终止时,您的服务也会被终止。更改 onStartCommand 的 return 值不会影响此过程。它只是告诉服务 start/stop 当你告诉它时或它完成它需要做的事情时。

要更改服务使其单独终止并假设它是启动服务而不是由于使用 onStartCommand 而绑定的服务,请在清单中为该服务指定一个进程名称。

来自Process and Threads Developer Guide

The manifest entry for each type of component element— <activity>, <service>, <receiver>, and <provider>— supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.

来自<service> in Manifest File

android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

使用这种方法,您的服务和您的应用程序将有两个不同的进程,导致它们因不同的原因被终止。 然而,这并不能保证它仍然不会被杀死。你应该设计、预期并愿意让你的服务被杀死。

此外,由于您的应用程序是音乐应用程序,并且用户积极参与使用您的服务,因此创建服务是使用 startForeground 为其提供前台优先级的可接受理由。但是,这并不意味着它永远不会被杀死。