1、APP为系统应用,需要签名;
2、APP位于/system/app下,才能接收到"android.intent.action.BOOT_COMPLETED"广播;
android:sharedUserId="android.uid.system"
package com.ted.leelenreboot;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
context.startService(new Intent(context, MyRebootService.class));
Log.i(GlobalParams.TAG, "start MyRebootService");
}
}
package com.ted.leelenreboot;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.format.Time;
import android.util.Log;
public class MyRebootService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
myHandler.postDelayed(myRunnable, GlobalParams.POST_DELAYED);
Log.i(GlobalParams.TAG, "MyRebootService onCreate");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
myHandler.removeCallbacks(myRunnable);
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void rebootSystem() {
Log.i(GlobalParams.TAG, "rebootSystem");
Intent reboot = new Intent(Intent.ACTION_REBOOT);
reboot.putExtra("nowait", 1);
reboot.putExtra("interval", 1);
reboot.putExtra("window", 0);
sendBroadcast(reboot);
}
private Handler myHandler = new Handler();
private Runnable myRunnable = new Runnable() {
@Override
public void run() {
Log.i(GlobalParams.TAG, "POST_DELAYED TIMEOUT");
if (need2Reboot()) {
rebootSystem();
return;
}
myHandler.postDelayed(myRunnable, GlobalParams.POST_DELAYED);
}
};
private boolean need2Reboot() {
if (SystemClock.uptimeMillis() > GlobalParams.TIMEOUT) {
long currentTimeMillis = System.currentTimeMillis();
Time now = new Time();
now.set(currentTimeMillis);
Time startTime = new Time();
startTime.set(currentTimeMillis);
startTime.hour = 2;
startTime.minute = 59;
Time endTime = new Time();
endTime.set(currentTimeMillis);
endTime.hour = 4;
endTime.minute = 1;
if (now.after(startTime) && now.before(endTime)) {
return true;
}
}
return false;
}
}
package com.ted.leelenreboot;
public class GlobalParams {
static final String TAG = "LeelenReboot";
static final long TIMEOUT = 864000000; // 10天
static final long POST_DELAYED = 1800000; // 0.5小时检测一次
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ted.leelenreboot"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.system" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".MyRebootService" >
</service>
<receiver android:name=".BootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>