如何启动一个服务,当Android设备打开?

如何启动一个service当Android设备打开和操作系统运行?

Solutions Collecting From Web of "如何启动一个服务,当Android设备打开?"

添加到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> 

创build类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); } }