简体   繁体   English

应用程序从最近的应用列表中删除后,Android服务崩溃

[英]Android service crashes after app is swiped out of the recent apps list

I have a service that gets started (not bound) by an activity. 我有一个由活动启动(未绑定)的服务。 If the activity gets destroyed (eg by pressing the back button), the service continues to run, this is of course intended. 如果活动被破坏(例如通过按下后退按钮),则服务继续运行,这当然是有意的。 However, if I swipe the activity out of the 'recent apps' list, the service gets restarted immediately. 但是,如果我将活动从“最近的应用”列表中滑出,则会立即重新启动该服务。 This is reproducible, every time the activity/app is swiped out of the list, there is a new call to the service's onCreate-method. 这是可重现的,每次从列表中删除活动/应用程序时,都会对服务的onCreate方法进行新调用。 No call to onDestroy in between! 两者之间没有调用onDestroy!

First I thought the service gets killed by android, even though I saw no reason for the kill (neither the activity nor the service do resource consuming things, in fact they are minimalistic and do nothing). 首先我认为服务被android杀死了,尽管我没有看到杀人的理由(活动和服务都没有资源消耗的东西,事实上它们是简约的,什么都不做)。 But then I noticed that the service actually crashes. 但后来我注意到服务实际上崩溃了。

V/MainActivity(856): onDestroy // swipe out of the list
I/ActivityManager(287): Killing 856:com.example.myapp/u0a10050: remove task
W/ActivityManager(287): Scheduling restart of crashed service com.example.myapp/.TestService in 5000ms

The code is not noteworthy, but here it is 代码并不值得注意,但在这里

Activity: 活动:

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.v(TAG, "onCreate, starting service...");
        startService(new Intent(this, TestService.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.v(TAG, "onStart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "onDestroy");
    }

    //[...]
}

Service: 服务:

public class TestService extends Service {

    private static final String TAG = "Service";

    // onBind omitted

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.v(TAG, "onDestroy");
    }

}

In short: My service is independent of the activity's lifecycle, but only as long as I don't swipe out the app of the recent apps list. 简而言之:我的服务独立于活动的生命周期,但只要我不刷新最近的应用列表的应用程序。 In that case, the service gets restarted but without a call to onDestroy. 在这种情况下,服务重新启动但没有调用onDestroy。

Every time this happens, not only the state of the service, but also the work the service is doing is lost. 每次发生这种情况时,不仅服务状态,而且服务正在进行的工作也会丢失。 I just want to know why the swipe is the reason for this. 我只是想知道为什么滑动就是这个原因。

Swiping the app from the recent tasks list actually kills the operating system process that hosts the app. 从最近的任务列表中滑动应用程序实际上会杀死托管应用程序的操作系统进程。 Since your service is running in the same process as your activities, this effectively kills the service. 由于您的服务在与您的活动相同的过程中运行,因此有效地杀死了该服务。 It does NOT call onDestroy() on the service. 它不会在服务上调用onDestroy() It just kills the process. 它只会杀死这个过程。 Boom. 繁荣。 Dead. 死。 Gone. 不见了。 Your service does not crash . 您的服务不会崩溃

Since your service returned START_STICKY from the call to onStartCommand() , Android recognizes that your service should be restarted and schedules a restart of the killed service. 由于您的服务从onStartCommand()调用返回START_STICKY ,因此Android会识别您的服务应该重新启动并安排重新启动已onStartCommand()的服务。 However, when your service is restarted it will be in a newly created process (you can see onCreate() called in the service), so it will have to start the work all over again. 但是,当您的服务重新启动时,它将处于新创建的进程中(您可以看到服务中调用的onCreate() ),因此它必须重新开始工作。

Rule #1: Don't ever swipe apps from the recent tasks list ;-) 规则#1:不要从最近的任务列表中刷过应用程序;-)

Maybe it can be a problem with Broadcast receivers defined in the manifest. 也许它可能是清单中定义的广播接收器的问题。

Do you have some receiver / intent-filter defined in your manifest at application level ? 您是否在应用程序级别的清单中定义了一些接收器/ intent-filter? I used to have same kind of problem and it was due to receiver declared in the manifest at the application level 我曾经有同样的问题,这是由于在应用程序级别的清单中声明的​​接收器

By swiping, your process is NOT guaranteed to be killed by the system get killed. 通过滑动,您的进程无法保证被系统杀死。 No. You only remove the applciation task (or back stack). 不。您只需删除applciation任务(或后台堆栈)。 Application task is NOT equal to the application process. 应用程序任务不等于应用程序进程。

So if you have any background jobs (threads, services etc) tied to your back stack and you have a good cancellation policy. 因此,如果你有任何后台工作(线程,服务等)绑定到你的后台堆栈,你有一个很好的取消政策。 The system may try to cache your process if it's suitable for later. 如果系统稍后适用,系统可能会尝试缓存您的进程。

If you kill the app process(es) from the Task Manager though, then it means that your process will be removed and so your JVM/sandbox aggressively by the system. 如果您从任务管理器中删除了应用程序进程,那么这意味着您的进程将被删除,因此系统会主动删除您的JVM /沙箱。

Use *START_NOT_STICKY* as onStartCommand return 使用* START_NOT_STICKY *作为onStartCommand返回

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(TAG, "onStartCommand");
    return START_NOT_STICKY;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Android中的最新应用中刷出应用后,服务停止 - Service stopped when app is swiped out from recent apps in Android 将应用程序从最近的应用程序列表中刷出后,处理耳机断开连接的情况 - handle headset disconnected after app is swiped out of the recent apps list 从小米的最近列表中清除后,服务无法重新启动 - Service not restart after swiped out from recent list in xiaomi 最近的活动将Android服务淘汰后,如何在后台运行Android服务? - How to run an Android service in Background after it is swiped out by Recent activity? 运行 BroadcastReceiver 即使应用程序从最近的列表中刷出 - Run BroadcastReceiver even when app is swiped out from recent list 从最近的列表中滑动应用程序后编写 Firebase 数据库 - Writing Firebase database after app is swiped from recent list 从最近列表中刷新应用时,未调用Android Firebase MyFirebaseMessagingService - Android Firebase MyFirebaseMessagingService not called when app is swiped from recent list 当应用程序从android中的最新应用程序列表中删除时,服务停止 - Service stopped when the app removes from recent apps list in android 在android生命周期中,当从最近的应用列表中滑动应用时,是否会调用任何独特的方法? - In android lifecycle, is there any unique method gets called when app is swiped from recent app list? 当应用程序从最近的应用程序窗口中滑出时取消通知 - Cancelling a notification when the app is swiped off the recent apps window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM