AlarmManager不添加新的警报,或不触发接收器

时间:2011-08-29 20:36:32

标签: java android alarmmanager

我目前正在开发一个使用Android的AlarmManager的应用程序。我正在尝试向AlarmManager添加一个新的警报,但经过几个小时的尝试各种事情并检查SO上的各种线程,我仍然在一堵砖墙。我的代码有问题吗?


主要活动 - saveAlarm()函数

/**
 * Saves the current alarm. Adds to the database if it doesn't already exist, or updates if it does.
 * Also sets alert with AlarmManager.
 */
public void saveAlarm() {
    // Create Database instance
    DbHandler db = new DbHandler(getApplicationContext());

    if(alarm.getId() == -1) {
        // Saving a new alarm
        db.open();
        alarm.setId(db.addAlarm(alarm));
        db.close();
    }
    else {
        db.open();
        db.updateAlarm(alarm);
        db.close();
    }

    // Create the wakeup intent
    Intent intent = new Intent(this, AlarmReceiver.class);
    intent.putExtra("alarm_id", alarm.getId());

    // Create the Pending Intent
    PendingIntent sender = PendingIntent.getBroadcast(this, AlarmPlayer.REQUEST_ALARM + alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Debug
    Calendar now = Calendar.getInstance();
    long dTime  = alarm.getNextAlarmTime().getTimeInMillis() - now.getTimeInMillis();
    Log.d(TAG, "Setting alarm for " + (dTime / 1000) + " seconds time");

    // Add to Android Alarm Manager
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, alarm.getNextAlarmTime().getTimeInMillis(), sender);    
}

我已经确认正确的时间传递到 am.set (请参阅上面的调试部分)。


AlarmReceiver.class

/**
* This class listens out for broadcasts from AlarmManager
* and launches the AlarmPlayer activity accordingly.
* @author Michael
*
*/
public class AlarmReceiver extends BroadcastReceiver {

     @Override
    public void onReceive(Context c, Intent intent) {
        Log.d("RSS Alarm", "Waking up alarm");
            // Launch the AlarmPlayer activity
        Intent i = new Intent(c, AlarmPlayer.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        c.startActivity(i);
    }

}

我还在AndroidManifest.xml中设置了<receiver android:process=":remote" android:name="AlarmReceiver" />。我不知道是什么导致了这个问题,但它仍在发生。非常感谢任何帮助,非常感谢提前。


修改1 将时区更改为UTC似乎无法解决任何问题,我的日历似乎默认为UTC。目前的代码:

// Debug
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
long dTime  = alarm.getNextAlarmTime().getTimeInMillis() - now.getTimeInMillis();
Log.d(TAG, "Setting alarm for " + (dTime / 1000) + " seconds time");

// Add to Android Alarm Manager
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Log.d(TAG, "Timezone offset is " + TimeZone.getDefault().getRawOffset());
Log.d(TAG, "UTC time is currently " + Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis() / 1000);
am.set(AlarmManager.RTC_WAKEUP, (alarm.getNextAlarmTime().getTimeInMillis() - TimeZone.getDefault().getRawOffset()), sender);

2 个答案:

答案 0 :(得分:0)

这可能是AlarmManager.set(AlarmManager.RTC_WAKEUP, ...)需要UTC时间的情况吗?如果您设置“本地时间”并且未调整为UTC,则会导致问题。简而言之,警报可能正在被正确添加,但除非您的时区是UTC,否则它不会在您预期时触发。

来自文档...

  

public static final int RTC_WAKEUP自:API Level 1

     

System.currentTimeMillis()中的闹钟时间(以UTC为单位的挂钟时间),它将在设备关闭时唤醒设备。

答案 1 :(得分:0)

在你的清单中,我相信你需要android:name“.AlarmReceiver”注意点。 如果不这样做,请发布清单文件。另外,你的logcat是否提到添加警报和警报触发?

编辑:您可以试试这个 AlarmManager am =(AlarmManager)getSystemService(Context.ALARM_SERVICE);

除此之外,我没有看到任何对我跳出来的事情。祝你好运