我正在使用CommonsWare的午餐列表示例来设置和重置警报。
我在特定时间设置闹钟,然后如果是重复闹钟,我会尝试将闹钟重置为新的日期/时间。
在OnAlarmReceiver中,我首先在应用程序的上下文(活动)中设置警报时尝试使用原始的三行代码。这三行是:
ComponentName component=new ComponentName(context, OnBootReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
OnBootReceiver.setAlarm(context, itemId, mDateDue);
然而,这似乎不起作用。我当时尝试添加这一行:
OnBootReceiver.cancelAlarm(context, itemId);
但这也没有区别。我没有正确理解这一切如何联系在一起,但我怀疑:
这个想法是,每次重复发生的警报发生时,它都会被代码重置。我知道我可以使用重复闹钟,但在我的应用程序的这个阶段,我更喜欢手动执行此操作。
这是OnAlarmReceiver:
public class OnAlarmReceiver extends BroadcastReceiver {
private static final int NOTIFY_ME_ID=1337;
private static final String TAG = "OnAlarmReceiver";
private DbAdapter mDbHelper;
private String mDateDue;
private String mFrequency;
@Override
public void onReceive(Context context, Intent intent) {
mDbHelper = new DbAdapter(context);
mDbHelper.open();
AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
Bundle bundle = intent.getExtras();
long itemId = bundle.getLong("itemId");
Cursor c = mDbHelper.getItem(itemId);
String itemTitle = c.getString(c.getColumnIndex(Db.KEY_ITEMS_TITLE));
int priority = c.getInt(c.getColumnIndex(Db.KEY_ITEMS_PRIORITY));
long listId = c.getLong(c.getColumnIndex(Db.KEY_ITEMS_LIST_ID));
String listTitle = mDbHelper.getListTitle(listId);
mDateDue = c.getString(c.getColumnIndex(Db.KEY_ITEMS_DATE_DUE));
mFrequency = c.getString(c.getColumnIndex(Db.KEY_ITEMS_FREQUENCY));
Toast.makeText(context, "Due: " + listTitle + "->" + itemTitle, Toast.LENGTH_LONG).show();
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
boolean useNotification=prefs.getBoolean("use_notification", true);
// Check if the alarm must be reset to a new future date based on frequency
checkResetAlarm(context, itemId);
if (useNotification) {
NotificationManager mgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
if (priority == 1) { // Display red icon
notification=new Notification(R.drawable.nuvola_apps_kwrite, itemTitle, System.currentTimeMillis());
} else { // Display blue icon
notification=new Notification(R.drawable.nuvola_apps_package_editors, itemTitle, System.currentTimeMillis());
}
Intent itemEditor = new Intent(context, ActivityEditItem.class);
long lAlarmId = (long) (int) itemId;
itemEditor.putExtra(DbAdapter.KEY_ITEMS_ITEM_ID, lAlarmId);
itemEditor.putExtra("listId", listId);
itemEditor.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// For flags, also see http://developer.android.com/guide/topics/ui/actionbar.html#ActionView
PendingIntent i=PendingIntent.getActivity(context, 0, itemEditor, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, listTitle, itemTitle, i);
String notifyPreference = prefs.getString("notification_sound", "DEFAULT_RINGTONE_URI");
notification.sound = Uri.parse(notifyPreference);
int oldVolume = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
if (priority == 1) {
Log.d(TAG, "A high priority item is due");
//notification.defaults |= Notification.DEFAULT_VIBRATE;
Vibrator v;
v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
int streamVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, streamVolume, 0);
}
mgr.notify((int) (long) itemId + NOTIFY_ME_ID, notification);
audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, oldVolume, 0);
}
else {
Intent i=new Intent(context, AlarmActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
private void checkResetAlarm(Context context, long itemId) {
if (!mFrequency.equals("")) {
String newDateDue = Item.addMinutesToDate(mDateDue, mFrequency);
Log.d(TAG, "Due date " + mDateDue + " reset with frequency of " + mFrequency + ", new due date: " + newDateDue);
mDbHelper.updateItemDueDate(itemId, newDateDue);
Toast.makeText(context, "Resetting alarm...", Toast.LENGTH_LONG).show();
ComponentName component=new ComponentName(context, OnBootReceiver.class);
context.getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
OnBootReceiver.cancelAlarm(context, itemId);
OnBootReceiver.setAlarm(context, itemId, mDateDue);
}
}
}
这是OnBootReceiver:
public class OnBootReceiver extends BroadcastReceiver {
public static void setAlarm(Context context, long itemId, String dateDue) {
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar cal=Calendar.getInstance();
String[] pieces=dateDue.split("/");
String day_of_month = dateDue.substring(8,10);
String hour = dateDue.substring(11,13);
String minute = dateDue.substring(14,16);
String second = dateDue.substring(17,19);
cal.set(Calendar.YEAR, Integer.parseInt(pieces[0]));
cal.set(Calendar.MONTH, Integer.parseInt(pieces[1])-1);
cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day_of_month));
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
cal.set(Calendar.MINUTE, Integer.parseInt(minute));
cal.set(Calendar.SECOND, Integer.parseInt(second));
cal.set(Calendar.MILLISECOND, 0);
if (cal.getTimeInMillis()<System.currentTimeMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 1);
}
mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
getPendingIntent(context, itemId));
}
/**
* Cancel Alarm
*
* @param ctxt
* @param itemId
*/
public static void cancelAlarm(Context ctxt, long itemId) {
AlarmManager mgr=(AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE);
mgr.cancel(getPendingIntent(ctxt, itemId));
}
private static PendingIntent getPendingIntent(Context ctxt, long itemId) {
//Intent i = new Intent(OnAlarmReceiver.ACTION, Uri.parse("timer:"+alarmId));
Intent i=new Intent(ctxt, OnAlarmReceiver.class);
i.putExtra("itemId", itemId);
return(PendingIntent.getBroadcast(ctxt, (int) (long) itemId, i, 0));
}
// When the phone restarts all alarms must be reset by this method
@Override
public void onReceive(Context ctxt, Intent intent) {
// To be added
//setAlarm(ctxt);
}
}
答案 0 :(得分:0)
愚蠢的编码错误,OnAlarmReceiver
我有OnBootReceiver.setAlarm(context, itemId, mDateDue);
而不是OnBootReceiver.setAlarm(context, itemId, newDateDue);
。
这篇文章也对我有所帮助:Android AlarmManager in a Broadcastreceiver