3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
kumar Sudheer
  • 705
  • 3
  • 8
  • 28
  • Are you getting any messages in logcat? – TheIT Dec 27 '13 at 04:04
  • 1
    I can say that the problem is, after the device has been rebooted, your app (and also your `BroadcastReceiver`) is not running, hence it cannot handle the next alarm. Compare when you use `Toast` instead, your app is not killed, it is still running and hence still showing the `Toast`. The solution I found is to run a `Service` on startup to register the `BroadcastReceiver` referred from [here](http://stackoverflow.com/a/12515718/2821954). I haven't tested it, but I believe this should solve the problem. – Andrew T. Dec 27 '13 at 04:06
  • Also, can I suggest you add a button to your application which also calls the rebootDevice() method to make sure it has nothing to do with the BroadcastReceiver. – TheIT Dec 27 '13 at 04:06
  • @AndrewT. when I call the BroadcastReceiver after reboot by handling in manifest file, as it contains only reboot method immediately it is going to reboot after starting the device... – kumar Sudheer Dec 27 '13 at 04:21
  • 2
    You need *additional* `Service`/`BroadcastReceiver` with `ACTION_BOOT_COMPLETED` intent-filter to register (but not call) your `AlarmReceiver`. – Andrew T. Dec 27 '13 at 04:40
  • @AndrewT. Yeh I am trying as you said, But I am not getting by this way. Can you plse edit my code as what you are thinking about this.. – kumar Sudheer Dec 27 '13 at 05:01

2 Answers2

1

See You'll have to detect the reboot of the device and have to fire the alarm once again in your onRecieve function like below:

EDIT:

You should have tried on your own. Anyway here you can do below :

public void onReceive(Context context, Intent arg1) {

boolean flag = Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction());

if(flag==true){
//call your set alarm function once again which you do in activity.
setAlarm(<Your Calendar time>, <your repeat boolean value>, context);

}else{

rebootDevice();


}

}

//make this function in your broadcast receiver class

private void setAlarm(Calendar targetCal, boolean repeat, Context ctx){

    Intent intent = new Intent(ctx, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, RQS_1, intent, 0);
    AlarmManager alarmManager = (AlarmManager)ctx.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);
    }

Hope this'll help.

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
  • Hai, we can't call the set alarm function here in another activity as the parameters i.e targetCal are from the onclick events on that activity.. – kumar Sudheer Dec 27 '13 at 05:46
  • Hi am not able to get that method using context in this activity, can you please help me with the above code.. – kumar Sudheer Dec 27 '13 at 06:17
  • After rebooting actually flag as to be true right, but arg1.getAction() is getting null, so it is not going into the loop and hence gain rebooting is not happening again... why arg1.getAction() is getting null every time.. – kumar Sudheer Dec 27 '13 at 12:34
  • Have you register your AlarmReceiver class with intent-filter action_BOOT_COMPLETED in manifest like below ? – TheLittleNaruto Dec 27 '13 at 12:38
  • yes, I used this one. as arg1.getAction() is getting null device is rebooting again without entering into loop. Can we make any changes in first class for this in alarmmanager method.. – kumar Sudheer Dec 27 '13 at 12:54
  • You'll have to manage your code , Man! In what condition you want to execute rebooting the device, I just simply called the function setAlarm(). Manage it at your end. – TheLittleNaruto Dec 27 '13 at 13:00
0

I just wanted to add that as @TheLittleNaruto and @Andrew T. have pointed out, the issue lies in that AlarmManager will clear all alarms when the device is rebooted as indicated in the docs:

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

http://developer.android.com/reference/android/app/AlarmManager.html

As has been pointed out, the solution is to register a boot receiver to reconfigure the alarm when the device reboots.

TheIT
  • 11,919
  • 4
  • 64
  • 56