每当应用在Android Pie中被杀死时,服务也会被杀死 [英] Service also gets killed whenever App is killed in android pie

查看:85
本文介绍了每当应用在Android Pie中被杀死时,服务也会被杀死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过创建一个android应用程序来学习Android编程.但是每当我杀死应用程序服务时,它也会被杀死.我正在使用 JobIntentService .使该应用程序在后台运行.

I'm learning Android programming by creating an android app. But whenever I kill the application service also gets killed. I'm using JobIntentService. To make that app work in the background.

JobIntentService类

public class BackGroundDistanceCalculate extends JobIntentService {

    final public static String TAG = "BackGroundDistance";

    static void enqueueWork(Context context, Intent job) {
        enqueueWork(context, BackGroundDistanceCalculate.class, 1, job);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Service Start");
    }

    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.d(TAG, "Execution Started");

        int i=0;

        for(i=0; i<1000; i++) {
            SystemClock.sleep(1000);
        }
    }

    @Override
    public boolean onStopCurrentWork() {
        Log.d(TAG,"Now It's Stopped");
        return super.onStopCurrentWork();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG,"Now It's Destroyed");
        super.onDestroy();
    }
}

MainActivity

Intent backCheck = new Intent(MapsActivity.this, BackGroundDistanceCalculate.class);
BackGroundDistanceCalculate.enqueueWork(this, backCheck);

创建此意图以在 onCreateMethod

清单

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<service
    android:name=".BackGroundDistanceCalculate"
    android:permission="android.permission.BIND_JOB_SERVICE" />

即使应用程序被终止,我可以进行哪些更改以使服务成功运行?预先感谢.

What are the changes that I can make to make service run successfully even when the app is killed? Thanks in advance.

推荐答案

好,您需要将服务迁移到前台服务或至少显示正在进行的通知,因为从android 8.0开始,应用程序无法在不通知用户的情况下运行后台服务.仍然,如果您希望在不通知的情况下,将您的服务迁移到辅助功能服务创建辅助服务.如果要创建一个链接,可以参考此链接.

well you need to migrate your service to foreground service or at-least show an ongoing notification because starting from android 8.0 onward apps cannot run background service without letting user know .Still if you want to go without notification one way is to migrate your service to Accessibility service Create accessibility service . You can refer to this link if you want to make one .

这篇关于每当应用在Android Pie中被杀死时,服务也会被杀死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆