I want to reboot the device when the app is running, at 2 am.
so I use PendingIntent
and AlarmManager
MainActivity.onCreate
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent("REBOOT_START_INTENT");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONT_SHOW);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
Manifest.xml
<receive android:name="RebootAlarm">
<intent-filter>
<action android:name="REBOOT_START_INTENT"/>
<intent-filter>
</receiver>
RebootAlarm.class
static Process rebootProcess;
@Override
public void onReceive(Context context, Intent intent) {
try {
rebootProcess = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
} catch (IOException e) {
e.printStackTrace();
}
}
this source, when app start. will always execute in RebootAlarm.class
so, when app start. will always execute reboot. why occur this problem?
I want only execute reboot on 2:00 am every day.
How to fix this problem?
thanks.