2

每个人都在这里说使用setExactAPI 级别 19 及更高级别,但找不到如何在其中设置重复时间,以便在指定时间再次重复。

那么请告诉我如何在setExact方法中设置重复时间?

这是我的代码

 Calendar calendar2,calendar,cal,cal2;
        calendar = new GregorianCalendar();
        calendar2 = new GregorianCalendar();

        calendar2.setTimeInMillis(System.currentTimeMillis());
        calendar.setTimeInMillis(System.currentTimeMillis());


        cal = new GregorianCalendar();
        cal2 = new GregorianCalendar();
        cal.add(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
        cal.set(Calendar.HOUR_OF_DAY, 10);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, calendar.get(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, calendar.get(Calendar.MILLISECOND));
        cal.set(Calendar.DATE, calendar.get(Calendar.DATE));
        cal.set(Calendar.MONTH, calendar.get(Calendar.MONTH));




        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent it = new Intent(MainActivity.this,Start_service_alarm.class);

        it.putExtra(Start_service_alarm.ACTION, Start_service_alarm.ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 987654321, it, 0);
        if(android.os.Build.VERSION.SDK_INT<19)
        {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }else
        {   
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

        }




        cal2.add(Calendar.DAY_OF_YEAR, calendar2.get(Calendar.DAY_OF_YEAR));
        cal2.set(Calendar.HOUR_OF_DAY, 20);
        cal2.set(Calendar.MINUTE, 0);
        cal2.set(Calendar.SECOND, calendar2.get(Calendar.SECOND));
        cal2.set(Calendar.MILLISECOND, calendar2.get(Calendar.MILLISECOND));
        cal2.set(Calendar.DATE, calendar2.get(Calendar.DATE));
        cal2.set(Calendar.MONTH, calendar2.get(Calendar.MONTH));


        AlarmManager alarmManager2 = (AlarmManager)getSystemService(ALARM_SERVICE);
        Intent itIntent = new Intent(MainActivity.this,Stop_service_alarm.class);

        itIntent.putExtra("ACTION_STOP", Stop_service_alarm.ACTION_STOP);
        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(MainActivity.this, 123456789, itIntent, 0);
        if(android.os.Build.VERSION.SDK_INT<19)
        {
        alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
        }else

        {
            alarmManager2.setExact(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), pendingIntent2);
        }
4

1 回答 1

-2

不幸的是,重复的预定时间不准确,我读到它们可能在整个间隔内都不准确!

如果您的 targetSdkVersion 为 19 或更高,则可以。

我可以做些什么来设置精确的重复警报?

使用 setExact() 来控制您的初始延迟。然后,作为处理该事件的工作的一部分,使用 setExact() 在下一个所需时间获得控制权。IOW,你自己做“重复”的部分。

是的,这很烦人。

引导开发人员不要使用精确的重复警报是故意令人恼火的,因为这些警报比不精确的兄弟更糟糕。如果用户会感知到不准确的行为并且不会欣赏它,请随意使用 setExact() 使警报在用户需要时发生。但是,如果用户不会察觉到不精确的行为,请使用不精确的警报,因为用户可能会注意到您的精确警报对电池的影响。

于 2015-05-14T08:47:16.987 回答