服务后自动关闭手机 [英] After the service automatically switched off phone
本文介绍了服务后自动关闭手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我计划提供一个可以在用户需要时启用和禁用的服务。
但是,如果服务正在运行,手机将关闭,除非用户再次激活它。
我想让手机再次上电后自动运行。
请帮助我成长。
非常感谢
Hi
I plan to have a service that can be enabled and disabled whenever the user wants.
But the phone is switched off if the service is running does not run unless the user activates it again.
I want the service to run automatically after power on the phone again.
Please help me grow.
Thank you very much
推荐答案
你需要创建一个BroadcastReceiver
并收听对于偶数RECEIVE_BOOT_COMPLETED
。
这将要求您的应用程序获得权限 android.permission.RECEIVE_BOOT_COMPLETE 。
You need to create aBroadcastReceiver
and listen for the evenRECEIVE_BOOT_COMPLETED
.
That will require your application to have permission android.permission.RECEIVE_BOOT_COMPLETE.
public class MyBootListener extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
final Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
这是一个显示如何自动启动一个活动 [ ^ ]您应该可以更改以启动您的服务。
希望这会有所帮助,
Fredrik
Here's an example showing how to auto start an Activity[^] that you should be able to change to start your service.
Hope this helps,
Fredrik
这篇关于服务后自动关闭手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文