如何在Android设备开启时启动服务?

时间:2011-10-05 23:13:43

标签: android

如何在启用Android设备并运行操作系统时启动service

1 个答案:

答案 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);       
    }

}