如何使应用程序在android中连续运行? [英] How to make an application run continuously in android?

查看:60
本文介绍了如何使应用程序在android中连续运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个将位置信息发送到服务器的应用程序.但是,当单击后退"或主页"按钮时,该应用程序将停止.有没有一种方法可以使应用程序在后台连续运行并将数据发送到服务器?

I am creating an application which sends the location information to the server. But when the back or home button is clicked the application is stopped. Is there a way to make the application run continuously in background and sends data to the server ?

推荐答案

使用背景服务.服务是可以在无需用户交互的情况下在后台运行的应用程序组件.android中有关 Service 的一个很好的教程来自.

Use a background Service. Services are application components which can run in the background without user interaction. A good tutorial on Service in android is from this.

您可能想在启动 Activity 时启动该服务,或者可以注册诸如 BOOT_COMPLETE 之类的系统广播,以保持服务从启动状态开始运行.

You probably want to start the service on launch of your Activity, or you can register for system broadcasts like BOOT_COMPLETE to keep your service running from boot up.

但是,在用户不知情的情况下保持服务始终运行始终是一个坏主意,并且还会消耗大量电池.您可能想使用 AlarmManager 唤醒服务,进行处理,安排下一次重新启动并退出.

However, keeping your service running throughout without user knowledge is a bad idea, and drains battery also. You probably want to wake up the service using AlarmManager, do processing, schedule next restart, and exit.

这篇关于如何使应用程序在android中连续运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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