创建即使在应用终止后仍可运行的Android应用服务 [英] Create a Android App Service which run even after app is terminated

查看:152
本文介绍了创建即使在应用终止后仍可运行的Android应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想在后台运行的服务

This is service which I wanted to run in the background

public class CustomMyService extends Service {

     public CustomMyService() {

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        return START_STICKY;
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent intent = new Intent("com.android.ServiceStopped");
        sendBroadcast(intent);
    }
}

清单文件

<service android:name=".CustomMyService">
            <intent-filter android:priority="1000">
                <action android:name="android.location.PROVIDERS_CHANGED"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </service>

请有人告诉我.我完成了所有的google和youtube搜索,没有任何效果.

Please kindly someone let me know. I did all google and youtube search, nothing is working.

我正在使用redmi note 3(其中有一个名为autostart的选项),如果我允许该应用程序进行自动启动,则服务将在后台运行.

Im using redmi note 3, in which I have a option called autostart, if I give permission to this app for autostart, service runs in background.

但是此选项在许多其他android智能手机中不存在,因此当应用终止时,应用服务将被终止.

But this option is not there in many of other android smart phone, so app services will be killed when app is terminated.

即使在应用终止后,也请告诉我如何运行服务.

Please kindly let me know how can service be running, even after app is terminated.

推荐答案

使用AlarmManager有时会激活您的服务.由于应用终止时服务将停止,因此需要再次吊销,并且警报管理可帮助您重新启动服务.

Use AlarmManager to envoke your service time to time. Because service will be stops when app is terminated.it's need to revoke again and Alarm manage helps to start your service again.

提到了许多用于连续服务的方法,但是在kitkat版本之后,这些方法没有帮助.

There are many methods mention for continuous service ,but those methods are not helpful after kitkat version.

如果即使在应用终止后仍能成功连续运行服务,请不要使用任何警报管理器(如调度方法).

If you have got success to run your service continuously even after app is terminated,without use of any Alarm Manager like scheduling methods please let me know.

这篇关于创建即使在应用终止后仍可运行的Android应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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