开机后自动重启的功能
来源:互联网 发布:linux 注释多行 编辑:程序博客网 时间:2024/05/01 11:17
定义一个类收android.intent.action.BOOT_COMPLETED广播的,并在30s后,发一个广播给RebooBroadcastReceiver类,重启机器。
public class AlarmScheduleReceiver extends BroadcastReceiver { // Restart service every 2 seconds private static final long REPEAT_TIME = 1000 * 2; static final String ACTION_COMP = "android.intent.action.BOOT_COMPLETED"; static final String TAG = "AlarmScheduleReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.v(TAG,"onReceive+"); if(intent.getAction().equals(ACTION_COMP)) { Log.v(TAG,"boot completed+"); AlarmManager service = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, RebootBroadcastReceiver.class); PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); Calendar cal = Calendar.getInstance(); // Start 30 seconds after boot completed cal.add(Calendar.SECOND, 30); // // Fetch every 30 seconds // InexactRepeating allows Android to optimize the energy consumption service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending); Log.v(TAG,"boot completed-"); // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), // REPEAT_TIME, pending); } Log.v(TAG,"onReceive-"); }}
重启广播类RebooBroadcastReceiver在OnReceive接口中要实现如下代码
public void onReceive(Context context, Intent intent) { Log.v(TAG, "OnReceive +");PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); pm.reboot("null"); Log.v(TAG, "OnReceive -"); }
另外要实现一个空的Activity,并且是应用启动的acitvity.
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="XXXXX" android:versionCode="1" android:versionName="1.0" android:sharedUserId="android.uid.system" > <uses-sdk android:minSdkVersion="15" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.REBOOT" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <receiver android:name=".AlarmScheduleReceiver" android:enabled="true" android:exported="true" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> <receiver android:name=".RebootBroadcastReceiver"> </receiver> <activity android:name=".AutoRebootActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>另外在Andorid.mk文件中要加入
LOCAL_CERTIFICATE := platform这样该应用就是系统的应用拉。
一切搞定,使用mm 编译即可,呵呵。
- 开机后自动重启的功能
- 开机后自动重启的功能
- ubuntu 开机后接着自动重启 //单用户模式如何进入//解决错误配置的方法
- cisco vpn client开机后自动拨号,断线后自动重拨的方法
- 电脑开机,开机后能进入系统,但是一会有自动重启了。
- Linux开机自动重启
- 开机后自动关机的功能
- 开机后自动关机的功能 .
- 手机的自动开机功能
- 关机后自动重启的解决办法
- C#中如何重启系统并在开机后自动执行当前可执行程序
- 程序的开机关机重启,开机启动,休眠功能delphi实现
- oracle开机自动重启配置
- android 平台开机自动重启
- Android 开机自动重启App代码
- linux 开机一段时间自动重启脚本
- 关闭计算机后自动开机的解决方法
- 开机后自动启动指定的服务
- JFreeChart API中文文档
- linux下socket编程
- linux基本监控-ss
- oracle 与 sql server语法函数比较
- linux基本监控 -iostat
- 开机后自动重启的功能
- mq入门实例
- 1595 hdu find the longest of the shortest
- 多线程使用中:对像被覆盖的BUG
- vim 分割窗口[转]
- linux基本监控 -sar
- eclipse配置TomCat服务器
- 转:Android自动化压力测试工具Monkey——测试例子
- JavaScript学习笔记-- Number数据类型