警报管理器 - 每2小时触发一次

时间:2014-10-27 07:23:03

标签: android alarmmanager

我有一段被调用一次的代码。但是,通知会在每2个小时后触发,因为我希望它在2天后触发。我尝试了所有选项,但仍无效

public int setAlarm(){
    Calendar Calendar_Object = Calendar.getInstance();
    /*
        Calendar_Object.set(Calendar.MONTH,6);
        Calendar_Object.set(Calendar.YEAR, 2013);
        Calendar_Object.set(Calendar.DAY_OF_MONTH, 18);*/

            // MyView is my current Activity, and AlarmReceiver is the
        // BoradCastReceiver
        Intent myIntent = new Intent(Splash.this, TimeAlarm.class);

           PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                    0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

            long wkupTime = Calendar_Object.getTimeInMillis() + 36000; 


        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//      Log.w("alarm set for " , Calendar_Object.getTime().toString ());

        /*
         * The following sets the Alarm in the specific time by getting the long
         * value of the alarm date time which is in calendar object by calling
         * the getTimeInMillis(). Since Alarm supports only long value , we're
         * using this method.
         */

//      alarmManager.setRepeating(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),1000 *24*3600,
//              pendingIntent);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, wkupTime , 172800000, pendingIntent);

        return 0;

1 个答案:

答案 0 :(得分:0)

public int setAlarm() {
    Calendar Calendar_Object = Calendar.getInstance();

    Intent myIntent = new Intent(Splash.this, TimeAlarm.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
                    0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    long wkupTime = Calendar_Object.getTimeInMillis() + 72000; 

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, wkupTime ,7200000, pendingIntent);

    return 0;
}