Android - Reset alarms when restarting the phone from SQLite database

I just started learning Android programming and am currently creating a simple reminder. Through some intro books, online blogs and, of course, many pages, I got it pretty well. Everything except the ability to reset my alarm when the phone reboots.

I have a BroadcastReceiver class that handles my alarms currently and has given it the correct permissions in my manifest to handle "RECEIVE_BOOT_COMPLETED". However, when I reset the phone, regardless of whether this alarm was set, it will start immediately after loading and have information about a zero value.

I found pages like this and, but I'm not sure how to get a calendar object and create it with the original values ​​of each alarm, and then pass on my receiver to set the alarm again ...

Here is what I still have.

The setAlarm method is used to create a one-time alarm from calendar values ​​that are set either by selecting a date and time, or predefined from a counter selection (10 minutes, tomorrow, etc.).

public void setAlarm(String alarmTitle, String alarmNotes, int alarmID) {
    // Launch a new intent to save the notification with the title and notes
    // passed from onSave
    Intent alarmIntent = new Intent(this, AlarmReceiver.class);
    alarmIntent.putExtra("Title", alarmTitle);
    alarmIntent.putExtra("Notes", alarmNotes);
    alarmIntent.putExtra("alarmID", alarmID);

    PendingIntent sender = PendingIntent.getBroadcast(this, alarmID,
            alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Pass alarm calendar to alarm manager
    am.set(AlarmManager.RTC_WAKEUP, aCal.getTimeInMillis(), sender);
}

Alarmreceiver

public class AlarmReceiver extends BroadcastReceiver {

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

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
    // Get a wake lock
    wl.acquire();

    // Get passed values
    Bundle extras = intent.getExtras();

    // Set values for use in this class
    String nTitle = extras.getString("Title");
    String nNotes = extras.getString("Notes");
    int uniqueID = extras.getInt("alarmID");

    // Set the intent to launch when the notification is clicked and allow the notifications to stack in the one row
    Intent resultIntent = new Intent(context, Main.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, uniqueID, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    long[] quickBurstsExtended = new long[]{0, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70,
            100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70, 100, 70};

    // Notification settings and alert time
    Notification builder = new NotificationCompat.Builder(context)
            .setContentTitle(nTitle) // Set the title
            .setContentText(nNotes) // Set notes
            .setSmallIcon(R.drawable.ic_launcher) // Set icon
            .setTicker("Did you remember: " + nTitle) // Marquee text (only briefly shown)
            .setContentIntent(resultPendingIntent) // Activty that is launched on click
            .setWhen(System.currentTimeMillis()) // Fire at the indicated time
            .setVibrate(quickBurstsExtended) // Allocate the above vibrate pattern
            .setLights(Color.CYAN, 500, 100) // LED lights control
            .build();

    builder.defaults = Notification.DEFAULT_SOUND;
    builder.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;

    NotificationManager notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Notify the manager when it is time
    notiManager.notify(uniqueID, builder);

    // Release the wakelock
    wl.release();
}

I store these reminders in a SQLite database and include an alarm column that stores a long calendar time value, for example: 20140129021559.

, "" , reset , , .

queueAll() SQLiteAdapter, ( ). ?

public Cursor queueAll() {
    String[] columns = new String[]{KEY_ID, KEY_TITLE, KEY_NOTES, KEY_TIME, KEY_DATE, KEY_ALARM_ID, KEY_ALARM};
    return db.query(DB_TABLE, columns, null, null, null, null, KEY_ALARM);
} 

. , , , , , .

, , , , .

.

EDIT: , .

, , , .

AlarmService, , action.BOOT_COMPLETED, AutoStart, , SQLite .

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

AutoStart, .

public class AutoStart extends BroadcastReceiver {
SQLiteAdapter db;

    @Override
    public void onReceive(Context context, Intent intent) {
        db = new SQLiteAdapter(context);

        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Cursor cursor = SQLiteAdapter.queueAll();
            while (cursor.moveToNext()) {
                    Date alarmDate = new Date(cursor.getLong(cursor.getColumnIndex(SQLiteAdapter.getKeyAlarm())));
                    Calendar aCal = Calendar.getInstance();
                    aCal.setTime(alarmDate);
                    String title = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.getKeyTitle()));
                    String notes = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.getKeyNotes()));
                    int alarmID = cursor.getInt(cursor.getColumnIndex(SQLiteAdapter.getAlarmID()));
                    AlarmUtils.setAlarm(title, notes, alarmID, context, aCal);
            }
        }
    }
}

.

!

+3

All Articles