3

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?

Mukesh Ram
  • 6,248
  • 4
  • 19
  • 37
fer
  • 87
  • 2
  • 9

3 Answers3

12

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

    }
}
Marat
  • 6,142
  • 6
  • 39
  • 67
  • ok thanks , but in every alarm intent i set a put extra " intent.putExtra("name", curso);" its just a string. but is the name of each course and i need that – fer Jul 14 '16 at 13:51
  • 1
    Its not problem. You can put any number of Extra to your intent. If you find this answer helpful, please Accept and Upvote it. I would appreciate that :) – Marat Jul 14 '16 at 14:24
  • Awesome answer. Thanks for taking the time to write it. – most venerable sir Oct 22 '18 at 13:00
0

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
    }
}
Fuyuba
  • 191
  • 1
  • 8
  • 1
    this i have to create a new receiver ??? or i should modify my – fer Jul 14 '16 at 04:59
  • public void onReceive(Context context, Intent intent) { //do what you want } in that part i need the data from the put extra how can i get it ???????? – fer Jul 14 '16 at 05:04
  • YourBootReceiver is new receiver that receive event phone reboot. And onReceive occur when the phone reboot completed, in that function you can set alarm again. I don't know what data you need to get there? – Fuyuba Jul 14 '16 at 06:36
0

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

Carol Cao
  • 3
  • 2