0

我正在使用 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);

但这也没有什么不同。我对这一切如何联系在一起没有正确的理解,但我怀疑:

  1. 我的上下文是错误的。
  2. 我必须对广播做点什么,比如取消它。
  3. 也许有一个需要更改的标志?

这个想法是,每次发生重复警报时,都会通过代码重置它。我知道我可以使用重复警报,但在我的应用程序的这个阶段,我更喜欢手动执行此操作。

这是 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);
}

}

4

1 回答 1

0

愚蠢的编码错误,在OnAlarmReceiver我有OnBootReceiver.setAlarm(context, itemId, mDateDue);而不是OnBootReceiver.setAlarm(context, itemId, newDateDue);.

这篇文章也帮助了我:Android AlarmManager in a Broadcastreceiver

于 2012-08-23T11:25:12.293 回答