Android 为什么我的服务被系统终止?

Android 为什么我的服务被系统终止?,android,service,android-lifecycle,foreground-service,Android,Service,Android Lifecycle,Foreground Service,我正在学习如何在Android中使用服务。我正在开发一个应用程序来了解他们。当用户按下“开始”按钮时,应用程序将启动服务。该服务将一直运行,直到用户按下“停止”按钮(类似Alpify或类似的应用程序) 在AndroidManifest中,我的服务声明如下: <service android:name="com.cpalosrejano.example.MyService" android:stopWithTask="false" android:enabled="

我正在学习如何在Android中使用服务。我正在开发一个应用程序来了解他们。当用户按下“开始”按钮时,应用程序将启动服务。该服务将一直运行,直到用户按下“停止”按钮(类似Alpify或类似的应用程序)

在AndroidManifest中,我的服务声明如下:

<service 
    android:name="com.cpalosrejano.example.MyService"
    android:stopWithTask="false"
    android:enabled="true" />
Intent service = new Intent(MyActivity.this, MyService.class);
startService(service);
然后我打开其他消耗大量内存的应用程序(Clash Royale、Instagram、Facebook等),我的服务被系统终止

我实现了
ontrimmory(int-level)
方法来查看发生了什么。在我的服务被终止之前,logcat会给我以下信息:

onTrimMemory() : 5 
onTrimMemory() : 10
onTrimMemory() : 15
我已经阅读了
ontrimmory()
方法的行为。文档中说,当我收到这些代码时,我必须释放未使用的对象但是我的服务还没有代码

我尝试的是:

  • 在AndroidManifest.xml文件中设置
    largeHeap=“true”
  • 使用
    startForeground()启动服务
  • START\u粘性
    服务中的标志
  • 获取一个
    wakelock
我的服务代码:

public class MyService extends Service {

    PowerManager.WakeLock mWakeLock;

    @Override
    public void onCreate() {
        Log.i(ServiceBase.class.getSimpleName(), "onCreate() : Service Started.");
        super.onCreate();
    }

    @Override
    public final int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(ServiceBase.class.getSimpleName(), "onStarCommand() : Received id " + startId + ": " + intent);

        // acquire wakelock
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, ServiceBase.class.getSimpleName());
        mWakeLock.acquire();

        // start foreground
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setSmallIcon(android.R.drawable.stat_sys_download);
        builder.setContentText("Service in foreground");
        builder.setContentTitle("My app");
        builder.setOngoing(true);
        Notification notification = builder.build();
        startForeground(1359, notification);

        // run until explicitly stopped.
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onBind() : true");
        return null;
    }

    @Override
    public void onRebind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onRebind() : true");
        super.onRebind(intent);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.i(ServiceBase.class.getSimpleName(), "onUnbind() : false");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.i(ServiceBase.class.getSimpleName(), "onDestroy()");
        super.onDestroy();
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Log.i(ServiceBase.class.getSimpleName(), "onTaskRemoved()");
        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onTrimMemory(int level) {
        Log.i(ServiceBase.class.getSimpleName(), "onTrimMemory() : " + level);
        super.onTrimMemory(level);
    }

}
现在,我的问题是:


如何可能,Runtastic应用程序正在运行,而我的应用程序正在被系统终止?

据我了解,与具有运行组件的进程相比,没有任何运行组件的进程将首先终止

Android可能会在某个时候决定关闭一个进程,因为内存不足 成本较低,且需要其他更直接的流程 为用户服务


请参考此链接

据我了解,与具有运行组件的进程相比,没有任何运行组件的进程将首先终止

Android可能会在某个时候决定关闭一个进程,因为内存不足 成本较低,且需要其他更直接的流程 为用户服务


请参考此链接

谢谢!我从未读过那篇文章。这是有用的。那么,你认为如果该服务开始检索GPS位置,Android不会杀死它,除非该设备在内存不足的情况下运行吗?谢谢!我从未读过那篇文章。这是有用的。那么,你认为如果该服务开始检索GPS位置,Android不会杀死它,除非该设备在内存不足的情况下运行吗?