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) {
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);
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, "");
wl.acquire();
Bundle extras = intent.getExtras();
String nTitle = extras.getString("Title");
String nNotes = extras.getString("Notes");
int uniqueID = extras.getInt("alarmID");
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 builder = new NotificationCompat.Builder(context)
.setContentTitle(nTitle)
.setContentText(nNotes)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Did you remember: " + nTitle)
.setContentIntent(resultPendingIntent)
.setWhen(System.currentTimeMillis())
.setVibrate(quickBurstsExtended)
.setLights(Color.CYAN, 500, 100)
.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);
notiManager.notify(uniqueID, builder);
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);
}
}
}
}
.
!