如何在设备启动时自动启动应用程序的服务(可以启用/禁用此功能)?我必须在AndroidManifest中包含哪些权限?
由于
答案 0 :(得分:5)
此权限是使用
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
在&lt; application&gt;中element(确保为BroadcastReceiver使用完全限定的[或相对]类名称):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在MyBroadcastReceiver.java中:
package com.example;
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
获取更多帮助:: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/ http://blog.gregfiumara.com/archives/82 http://androidgps.blogspot.com/2008/09/starting-android-service-at-boot-time.html