3

I have set an alarm at specific time. If the phone is turn off the alarm is lost? I have turn off the phone, start it again but the alarm did not trigger at the specified time.

PendingIntent pendingIntent = PendingIntent.getBroadcast(
            Global.a.getApplicationContext(), (int) cd.e.alarmTime, intent, 0);

    AlarmManager alarmManager = (AlarmManager) Global.a.getSystemService(Activity.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, cd.e.alarmTime, pendingIntent);
Vítor Marques
  • 349
  • 4
  • 11

3 Answers3

8

First you need to create a BroadcastReceiver to trigger on BOOT_COMPLETE

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
            if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
                    Intent mServiceIntent = new Intent();
                    mServiceIntent
                                    .setAction("com.crossfire.appname.service.BootService");
                    ComponentName service = context.startService(mServiceIntent);
                    if (null == service) {
                            // something really wrong here
                            Log.e(TAG, "Could not start service ");
                    }
            } else {
                    Log.e(TAG, "Received unexpected intent " + intent.toString());
            }
    }
}

Then create a service to reset all your alarms:

public class BootService extends Service {

    private final String TAG = "BootService";

    @Override
    public IBinder onBind(final Intent intent) {
            return null;
    }

    @Override
    public void onCreate() {
            super.onCreate();
            Trace.i(TAG, "created");
    }

    @Override
    public void onStart(final Intent intent, final int startId) {
            super.onStart(intent, startId);
            Trace.i(TAG, "started");

            LoadAlarmsFromDatabase();

    }

    private void LoadAlarmsFromDatabase() {
           Cursor c = mDbHelper.getPendingAlarms();
            if (c.moveToFirst()) {
                    do {

                            int id = c.getInt(mDbHelper.ID_COLUMN);
                            String time = c.getString(mDbHelper.TIME_COLUMN);
                            addNotification(id, time);
                    } while (c.moveToNext());
            }
            c.close();
    }

    private void addNotification(int apptId, String time) {
            Trace.d(TAG, "addNotification " + apptId + ", " + time);
            Intent intent = new Intent(BootService.this,
                            ApptBroadcastReceiver.class);

            intent.putExtra("appt_id", Integer.toString(apptId));

            PendingIntent mAlarmSender = PendingIntent.getBroadcast(
                            BootService.this, apptId, intent, 0);

            long alarmTime = System.currentTimeMillis() + 60000;
            Calendar c = null;

            try {
                    c = CustomDateUtils.StringToCalendar(time);
                    alarmTime = c.getTimeInMillis();
            } catch (ParseException e) {
                    Log.e(TAG, "ParseException");
            }

            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, alarmTime, mAlarmSender);

    }
}

Finally, add the permission and receiver to the manifest

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
 <receiver android:name=".receiver.BootReceiver" android:enabled="true">
            <intent-filter>
                    <action android:name ="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
    </receiver>
Woodsy
  • 3,177
  • 2
  • 26
  • 50
  • Sorry @Woodsy, I have a stupid question why you run the query directly inside the OnStart method? Isn't it operetion which need to be done asyncrounusly? – Matteo Corti Dec 11 '14 at 18:06
4

Tried @Woodsy Method but didn't work so i fixed some things so it can work

public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {

        Log.d("receiver", "The Receiver is called");
        Intent mServiceIntent = new Intent(context, BootService.class);
        context.startService(mServiceIntent);

    } else {
        Log.d("boot receiver ",
                "Something went wrong with the boot complete maybe with permissions ");
    }
}

}

*Permissions & Manifest *

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



Booting update 
        <service android:name="com.packagename.appname.service.BootService" />

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

Hope this could help others

Yasin Hassanien
  • 4,055
  • 1
  • 21
  • 17
0

You could do it like this:

Store the time in a SharedPreference, catch Intent.ACTION_BOOT_COMPLETED and set the AlarmManager again if required.

zapl
  • 63,179
  • 10
  • 123
  • 154