I need to restart the device pragmatically using Alarm manager, I am calling the intent and in the intent activity I am using reboot function and my device is rooted one. My code is as follows
public void onClick(View v) {
Calendar calNow = Calendar.getInstance();
Calendar calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
calSet.set(Calendar.MINUTE, timePicker.getCurrentMinute());
calSet.set(Calendar.SECOND, 0);
setAlarm(calSet, optRepeat.isChecked());
}});
private void setAlarm(Calendar targetCal, boolean repeat){
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis() , pendingIntent);
if(repeat){
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),
TimeUnit.MINUTES.toMillis(2),pendingIntent);
}else{
alarmManager.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),pendingIntent);
}
}
And in AlarmReceiver.java, I build up the code as
public class AlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
rebootDevice();
}
And in Manifest file, my code is like
<activity android:name=".activity.RebootTimer" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog" ></activity>
<receiver android:name=".service.AlarmReceiver" />
For first time my device is rebooting as what I am required, after interval my device is not rebooting again. When I kept the toast message by hiding reboot function, toast message is displaying for every interval.