为什么我的服务被系统杀死?

时间:2017-05-19 08:50:31

标签: android service android-lifecycle foreground-service

我正在学习如何在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等等),我的服务被系统杀死。

我实施了onTrimMemory(int level)方法来查看正在发生的事情。在我的服务被杀之前,logcat会给我以下信息:

onTrimMemory() : 5 
onTrimMemory() : 10
onTrimMemory() : 15

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

我尝试了什么:

  • 在AndroidManifest.xml文件中设置largeHeap="true"
  • 使用startForeground()
  • 启动服务 服务中
  • START_STICKY标志
  • 获得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应用程序是如何运行的?

1 个答案:

答案 0 :(得分:2)

根据我的理解,与具有正在运行的组件的进程相比,没有任何运行组件的进程将首先被终止。

  

Android可能决定在内存时关闭某个进程   很低,并且需要更直接的其他流程   为用户服务。

请参阅此链接https://developer.android.com/guide/components/processes-and-threads.html

相关问题