这个问题在这里已经有了答案:
Best Answer-推荐答案 strong>
首先你需要权限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
然后创建一个广播接收器为
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Startup extends BroadcastReceiver {
public Startup() {
}
@Override
public void onReceive(Context context, Intent intent) {
// start your service here
context.startService(new Intent(context, SERVICE.class));
}
}
在您的 list 中将此广播接收器注册为
<receiver android:name="YOUR_PACKAGE.Startup" >
<!-- This intent filter receives the boot completed event -->
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Note: A service too is not guaranteed to run from device boot to device
shut down, as in extreme cases the Android system may kill the service
also to gain additional memory.
关于java - 如何在android中的设备启动时启动服务?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/20595337/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) |
Powered by Discuz! X3.4 |