如何在启用Android设备并运行操作系统时启动service
?
答案 0 :(得分:24)
添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".YourService" />
<receiver android:name="com.your.package.AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
创建类AutoStart.java:
public class AutoStart extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent startServiceIntent = new Intent(context, YourService.class);
context.startService(startServiceIntent);
}
}