内容简介:翻译自:https://stackoverflow.com/questions/11168869/starting-background-service-when-android-turns-on
应用程序和服务器.我知道如何通过我的应用程序启动它,但是当Android关闭时,后台服务将会死亡.
如何使后台服务始终运行? (即使设备关闭然后再打开……)
我需要添加Android的后台服务的启动程序.任何提示?
action android:name =“android.intent.action.BOOT_COMPLETED”/>在设备开启时启动服务.
在AndroidManifest.xml中:
<receiver android:name=".BootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在AndroidManifest.xml中添加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"> </uses-permission>
在代码部分BootBroadcastReceiver中:
public class BootBroadcastReceiver extends BroadcastReceiver {
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// BOOT_COMPLETED” start Service
if (intent.getAction().equals(ACTION)) {
//Service
Intent serviceIntent = new Intent(context, StartOnBootService.class);
context.startService(serviceIntent);
}
}
}
编辑:如果您正在谈论设备屏幕开/关,那么您需要注册<action android:name =“android.intent.action.USER_PRESENT”/>和<action android:name =“android.intent.action.SCREEN_ON”/>用户出现或屏幕打开时启动服务.
翻译自:https://stackoverflow.com/questions/11168869/starting-background-service-when-android-turns-on
以上所述就是小编给大家介绍的《Android打开时启动后台服务》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Python语言程序设计
(美)Y. Daniel Liang / 机械工业出版社 / 2013-3 / 79.00元
本书保持了Liang博士系列丛书中一贯的、标志性的教与学的哲学:以实例教,由实践学。书中采用了他所提出的已经经过实践检验的“基础先行”的方法,即在定义类之前,首先使用清晰简明的语言介绍基本程序设计概念,如选择语句、循环和函数;在介绍面向对象程序设计和GUI编程之前,首先介绍基本逻辑和程序设计概念。书中除了给出一些以游戏和数学为主的典型实例外,还在每章的开始使用简单的图形给出一两个例子,以激发学生的......一起来看看 《Python语言程序设计》 这本书的介绍吧!