Android的 - 定期进行后台服务 - 咨询 [英] Android - Periodic Background Service - Advice

查看:102
本文介绍了Android的 - 定期进行后台服务 - 咨询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,将中继关于它的位置到远程服务器的信息。我打算做一个简单的HTTP发布到网络服务器来做到这一点,一切都简单而精致的。

I am working on an app that will relay information about its location to a remote server. I am intending to do it by doing a simple HTTP post to the web-server and all is simple and fine.

不过,根据该规范,应用程序需要从时间执行自己的时间,可以说,每30分钟研究。是独立的,它需要即使在应用程序被关闭运行界面的意思。

But according to the spec, the app needs to execute itself from time to time, lets say once in every 30 mins. Be independent of the interface, meaning which it needs to run even if the app is closed.

我环顾四周,发现,Android的服务是需要使用。我能用来实现这样的系统。请问服务(或其他机构),重启手机重启时?

I looked around and found out that Android Services is what needs to be used. What could I use to implement such a system. Will the service (or other mechanism) restart when the phone restarts?

在此先感谢。

推荐答案

创建一个服务来您的信息发送到服务器。 presumably,你已经得到了控制。

Create a Service to send your information to your server. Presumably, you've got that under control.

服务应该由<一个触发的警报启动href="http://developer.android.com/reference/android/app/AlarmManager.html"><$c$c>AlarmManager,在这里你可以指定的时间间隔。除非你有报告数据的完全的每30分钟,你可能想不精确的报警,所以你可以节省一些电池寿命。

Your Service should be started by an alarm triggered by the AlarmManager, where you can specify an interval. Unless you have to report your data exactly every 30 minutes, you probably want the inexact alarm so you can save some battery life.

最后,您可以注册应用程序,以获得启动广播通过设置<一个href="http://developer.android.com/reference/android/content/BroadcastReceiver.html"><$c$c>BroadcastReceiver像这样:

Finally, you can register your app to get the bootup broadcast by setting up a BroadcastReceiver like so:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {  
        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            // Register your reporting alarms here.            
        }
    }
}

您需要将以下权限添加到您的的Andr​​oidManifest.xml 对于工作。不要忘了,当你正常运行的应用程序,以注册报警,否则他们会只进行登记时,设备启动。

You'll need to add the following permission to your AndroidManifest.xml for that to work. Don't forget to register your alarms when you run the app normally, or they'll only be registered when the device boots up.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

这篇关于Android的 - 定期进行后台服务 - 咨询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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