0

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.

hyunwookcho
  • 147
  • 1
  • 3
  • 16
  • 1
    You're not accounting for the possibility that 2:00am has already passed today, in which case you're setting an alarm in the past, and it fires immediately, give or take a few seconds. – Mike M. Nov 29 '18 at 09:12
  • The answer on the linked duplicate shows how to check the alarm time against the current time, and add a day if it's already passed today. – Mike M. Nov 29 '18 at 09:15
  • 1
    @MikeM. Thanks you – hyunwookcho Nov 30 '18 at 00:30

0 Answers0