首页 > 解决方案 > 杀死应用程序后启动的服务被杀死

问题描述

从最近的应用程序中删除该应用程序后,该服务会被终止。但这不应该被杀死并始终在后台运行。我看到应用程序打开时服务正在运行。当我通过主页按钮最小化应用程序时,它仍在运行。但是如果我如上所述杀死它,它将停止。我该如何解决这个问题?

public class NotificationService extends Service {


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



    @Override
    public void onCreate() {
        Toast.makeText(getApplicationContext(),"service started",Toast.LENGTH_LONG).show();



    }

    @Override
    public void onDestroy() {
        Toast.makeText(getApplicationContext(),"service stopped",Toast.LENGTH_LONG).show();

    }

    @Override
    public int onStartCommand(final Intent intent,
                              final int flags,
                              final int startId) {
        onTaskRemoved(intent);
        //code
        return  START_STICKY;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {


        Intent myIntent = new Intent(getApplicationContext(), NotificationService.class);
        myIntent.setPackage(getPackageName());
        startService(myIntent);
        super.onTaskRemoved(rootIntent);


    }


}

在前台服务的情况下,我不想要任何服务通知。

标签: androidandroid-servicebackground-service

解决方案


我得到了答案,应用程序的自动启动已关闭,因此服务一旦被终止就无法重新启动。谢谢大家的时间!


推荐阅读