I have to reboot the device on specific time.i am using below code for that
private void rebootAfterSomeTime() {
h = new Handler(Looper.getMainLooper());
r = new Runnable() {
public void run() {
//current time
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int sec = c.get(Calendar.SECOND);
String currenttime = String.valueOf(hour) + " : " + String.valueOf(min) + " : " + String.valueOf(sec);
Log.d("Gajanand", "run: "+currenttime);
//comparing current time with 12:00pm
if (currenttime.equals("23 : 59 : 59")) {
//reboot the device
rebootDevice();
}
h.postDelayed(this, delayMillis);
}
};
h.post(r);
}
i am rebooting the device with two methods one by using powerManager and another by using SU..like below
private void systemAppsrebootOnly() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
pm.reboot(null);
}
public static void rebootDevice() {
try {
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("reboot \n");
} catch (Throwable t) {
t.printStackTrace();
}
}
Yes in both the cases device is rebooting properly.but the BroadcastReceiver is not triggering in both the case. but when i reboot device manually by long pressing power button and reboot that time BroadcastReceiver is triggering. here is my ShutdownReceiver
public class ShutdownReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/*if(intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED"))
{
Toast.makeText(context, "boot_completed", Toast.LENGTH_SHORT).show();
}
else*/
// Toast.makeText(context, ""+intent.getAction(), Toast.LENGTH_SHORT).show();
Log.e("kishan", "onReceive:mPowerReceiver powerofffffffffff");
ManvishPrefConstants.SHUTDOWN_TRIGGERED.write(true);
ManvishPrefConstants.SHUTDOWN_DATE_TIME.write(CommanUtils.formatDate(System.currentTimeMillis()));
}
}
Small part of my manifest file.i have added all necessary permissions.
<receiver android:name=".Activities.ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN"/>
<action android:name="android.intent.action.ACTION_REBOOT"/>
<action android:name="android.intent.action.QUICKBOOT_POWEROFF"/>
</intent-filter>
</receiver>
any help ?