参考的是u0fly 感谢u0fly,您写的很仔细,比起很多人。。。 地址:http://blog.csdn.net/u0fly/archive/2010/09/27/5909477.aspx
依然是介绍我自己用到的代码,我的理解就是先建一个继承BroadcastReceiver的BootReceiver类来启动AlarmService这个服务,com.overflow.AlarmService就是在manifest里定义的这个包里的服务,然后看这个类的代码:
public class AlarmBootReceiver extends BroadcastReceiver { static final String ACTION = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub if(arg1.getAction().equals(ACTION)) { Intent intent = new Intent(arg0,AlarmService.class); intent.setAction("com.overflow.AlarmService"); arg0.startService(intent); } } }
然后就是在manifest这里面把这个bootreceiver和bootservice给注册下,记住位置不要写错
<service android:name="AlarmService"> <intent-fliter> <action android:name="com.overflow.AlarmService" /> </intent-fliter> </service> <receiver android:name=".AlarmBootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
最后依然在manifest里补上权限:<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>