fer
fer

Reputation: 87

android alarmmanager alarms after reboot 2016

I making a app, where you can set alarms. For example, I have class on Mondays at 7o' clock, so I need to start the alarm every Monday, but also I have another class on Tuesday and have to do it the same.

I already can do that and works, with an alarm for each curso, but when I reboot the cellphone then they don't work .

Here is my code the put extra is so important in my app:

Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, horai);
    cal.set(Calendar.MINUTE, minutoi);
    cal.set(Calendar.DAY_OF_WEEK, dias.getSelectedItemPosition() + 1);
    cal.add(Calendar.SECOND, 2);



    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    intent.putExtra("name", curso);
    //intent.putExtra("curos bn",1);
    PendingIntent pendingIntent =
           PendingIntent.getBroadcast(getBaseContext(),
           cont, intent, PendingIntent.FLAG_UPDATE_CURRENT );//cont star with 1 a then ++

RECEIVER

public class AlarmReceiver extends BroadcastReceiver {

private static final String TAG = "BANANEALARM";
Intent intent;
PendingIntent pendingIntent;
NotificationManager notificationManager;
private static final int PERIOD=5000;
@Override
public void onReceive(Context context, Intent intent) {



    Log.i(TAG, "BroadcastReceiver has received alarm intent.");
    Intent service1 = new Intent(context, AlarmService.class);
    String id = intent.getStringExtra("name"); // this is so important
    service1.putExtra("name",id);
    context.startService(service1);

}

Manifets

  <receiver android:name=".AlarmReceiver"  android:enabled="true" >

  </receiver>
  <service android:name=".AlarmService" />

So in other part of my application I set alarms with data that I get from a json. All works like I want the only problem is when I reboot the phone?

Upvotes: 3

Views: 5304

Answers (3)

Marat
Marat

Reputation: 6703

According to developer.google.com

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.

Therefore, you will need to create another receiver that would recreate all your alarms. This receiver is not your AlarmReceiver, it does not handle activated alarms. It is used only to reset them after reboot.

To do so you need these code lines:

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application

    <!-- rest of the code -->

    <receiver android:name=".AlarmBroadcastReceiver"/>

    <service android:name=".BootService"/>

    <receiver android:name=".RestartAlarmsReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

RestartAlarmsReceiver.java

public class RestartAlarmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

            // It is better to reset alarms using Background IntentService
            Intent i = new Intent(context, BootService.class);
            ComponentName service = context.startService(i);

            if (null == service) {
                // something really wrong here
                Log.e(TAG, "Could not start service ");
            }
            else {
                Log.e(TAG, "Successfully started service ");
            }

        } else {
        Log.e(TAG, "Received unexpected intent " + intent.toString());
        }
    }
}

BootService.java

public class BootService extends IntentService {

    public BootService() {
        super("BootService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Your code to reset alarms.
        // All of these will be done in Background and will not affect
        // on phone's performance

    }
}

Upvotes: 12

Carol Cao
Carol Cao

Reputation: 3

The kernel trigger the alarm when your alarm times out, but it can't save your alarm's status after reboot. So you should reset your alarm if the user reboots the device.

Android developer gives your answer here

Upvotes: 0

Fuyuba
Fuyuba

Reputation: 191

You can listen event reboot, then do what you want when the phone reboot

In your manifest

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

...

<receiver android:name=".YourBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>

Your java code

public class YourBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //do what you want
    }
}

Upvotes: 0

Related Questions