android实现开机自启动可能是移动操作系统中最简单的了,我们只需要监听一个开机启动的Broadcast(广播)即可。首先写一个Receiver(即广播监听器),继承BroadcastReceiver,如下所示:
01 | public class BootReceiver extends BroadcastReceiver { |
02 |
private PendingIntent mAlarmSender; |
04 |
public void onReceive(Context context, Intent intent) { |
06 |
mAlarmSender = PendingIntent.getService(context, 0, new Intent(context, |
07 |
RefreshDataService. class ), 0); |
08 |
long firstTime = SystemClock.elapsedRealtime(); |
09 |
AlarmManager am = (AlarmManager) context |
10 |
.getSystemService(Activity.ALARM_SERVICE); |
11 |
am.cancel(mAlarmSender); |
12 |
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, |
13 |
30 * 60 * 1000, mAlarmSender); |
接下来,我们只需要在应用程序配置文件AndroidManifest.xml中注册这个Receiver来监听系统启动事件即可,如下所示:
view source print ?
1 | <receiver android:name= ".service.BootReceiver" > |
4 |
<action android:name= "android.intent.action.BOOT_COMPLETED" > |
- 一、 Android完全退出应用程序
- Android应用程序绑定服务(bindService)的过程
- 四十二、Android完全退出应用程序
- Eclipse 开发Android 出现 aapt.exe 应用程序错误,该内存不能为“
- android中如何隐藏应用程序标题栏和通知栏
- android 彻底关闭应用程序
- 一、 Android 应用程序概述
- Android的应用程序框架
随机推荐
-
TextView——文本省略显示
-
android:contentDescription 的用途
-
android:fitsSystemWindows
-
Android常用adb命令总结
-
framework的调试与编译方法
-
Android(安卓)application context/activ
-
android中padding和margin的区别
-
android 中生成xml文件
-
android EditText imeOptions属性和各属
-
[Android(安卓)中级]Voip之CSipSimple类