关闭Android活动后的后台服务

时间:2017-09-04 09:58:10

标签: android service background-process

我尝试创建应用程序,检查一些远程数据库的新闻和活动。当应用程序运行时,它工作正常,应用程序检查,显示通知等。但如果用户关闭应用程序,一切都会停止。是否可以在应用程序中创建后台服务,该应用程序在应用程序关闭后工作(例如,用于发送本地通知)?如果有可能,我必须做什么?

P.S。我了解Firebase和Google Cloud,但我想创建独立的应用程序。

2 个答案:

答案 0 :(得分:4)

As @Flo We said Service is the only option you can use

public class BackGroundService extends Service{
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Service","Started");

//Write your code here to run in background


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

}

使用Intent启动服务,不要在onDestroy上停止服务,它将继续在后台运行

答案 1 :(得分:0)

Services几乎是你唯一的选择。但是你永远都不知道你的服务什么时候会被杀死,而且随着Android的发展它会越来越严格。