开机后自动重启的功能

来源:互联网 发布: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 编译即可,呵呵。

原创粉丝点击