简体   繁体   English

从“最近使用的应用程序”列表中删除应用程序后,Android服务将重新启动

[英]Android service gets restarted when application is removed from Recent Apps list

Code to start the service on button click 单击按钮即可启动服务的代码

public void serviceBtnClicked(View view) {
    SharedPreferences sharedPreferences = getSharedPreferences("my.package.name", MODE_PRIVATE);
    if (!sharedPreferences.getBoolean("_done", false) && !isMyServiceRunning(MyService.class)) {
        startService(new Intent(this, MyService.class));
    }
}

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

MyService.java MyService.java

@Override    
public void onCreate() {
    Log.d("_pop", "create");
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    new MyFirstTask().execute();
    return super.onStartCommand(intent, flags, startId);
}

public class MyFirstTask extends AsyncTask<String, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... params) {
        doStuff(0);
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        SharedPreferences sharedPreferences = getSharedPreferences("my.package.name", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("_done", true);
        editor.apply();       
        super.onPostExecute(aVoid);
    }
}

When I click the button, the service starts as expected and does all the work properly. 当我单击按钮时,该服务将按预期方式启动,并且可以正常进行所有工作。 But the problem is when I remove my application from the Recent Apps list the service get started again . 但是问题是, 当我从“最近使用的应用程序”列表中删除我的应用程序时,该服务又重新启动了 One interesting thing is that when I restart the application, the service does not starts which means that the SharedPreferences _done boolean check is working. 一件有趣的事是,当我重新启动应用程序时,该服务未启动,这意味着SharedPreferences _done布尔检查正在运行。

Ideally, you would replace your entire service with an IntentService , getting rid of your AsyncTask (not used by services) and putting your doInBackground() and onPostExecute() code into onHandleIntent() . 理想情况下,您可以将整个服务替换为IntentService ,摆脱AsyncTask (服务不使用),并将doInBackground()onPostExecute()代码放入onHandleIntent()

Beyond that, replace: 除此之外,请替换:

return super.onStartCommand(intent, flags, startId);

with: 有:

return START_NOT_STICKY;

as the default of onStartCommand() is to start a sticky service, meaning that the service will be restarted automatically some time after your process is terminated. 因为onStartCommand()的默认设置是启动粘性服务,这意味着该服务将在您的进程终止后的某个时间自动重新启动。

暂无
暂无

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

相关问题 当应用程序从android中的最新应用程序列表中删除时,服务停止 - Service stopped when the app removes from recent apps list in android 从Android中的最新应用中刷出应用后,服务停止 - Service stopped when app is swiped out from recent apps in Android Android - 从最近的应用程序中滑动应用程序时停止服务 - Android - Service stops when swiping app from recent apps 从最近的应用程序中删除应用程序后,IntentService停止工作 - IntentService stops working when app is removed from recent apps 如何避免我的android应用程序显示在“最近的应用程序”列表中? - How to avoid my android application to be shown in the “recent apps” list? 在android生命周期中,当从最近的应用列表中滑动应用时,是否会调用任何独特的方法? - In android lifecycle, is there any unique method gets called when app is swiped from recent app list? 即使应用程序被杀死,我怎样才能让我的应用程序运行? 从最近的应用程序中删除时在某种意义上被杀死 - How can i make my app run even when app is killed? killed in the sense when it is removed from recent apps 从打开的应用程序中删除应用程序时,前台服务有时会重新启动,有时永远不会启动 - Foreground service is sometimes restarting and sometimes never starts when app is removed from open apps Android-重新启动服务时如何保留套接字连接 - Android - How to preserve socket connection when a service is restarted Android 游戏 - 当实体从列表中移除时屏幕闪烁 - Android Game - Screen Flickers When an Entity Removed From The List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM