Android AlarmManager未按计划时间启动?

Android AlarmManager未按计划时间启动?,android,android-broadcastreceiver,Android,Android Broadcastreceiver,我已经在android应用程序中实现了AlarmManager。报警的重复计划时间为5秒。但不是5秒警报,而是在一分钟后重复 AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); intent.putExtra(Constants.Extra.SERVICE_TYPE, Constants.Extra.SERVICE_TRIGGER); PendingIntent p

我已经在android应用程序中实现了AlarmManager。报警的重复计划时间为5秒。但不是5秒警报,而是在一分钟后重复

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

intent.putExtra(Constants.Extra.SERVICE_TYPE, Constants.Extra.SERVICE_TRIGGER);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 123456789, intent, 0);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
接收级

@Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if(extras != null){
            String type = extras.getString(Constants.Extra.SERVICE_TYPE);

            if(type != null && type.equalsIgnoreCase(Constants.Extra.SERVICE_TRIGGER)){

                //Triger  

            }
        }
    }
manifest.xml文件

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- Receivers -->
<receiver android:name=".receiver.ServiceTrigger">
    <intent-filter>
        <action android:name="com.project.ServiceTrigger" />
    </intent-filter>
</receiver>

这对我很有用

用这个

 alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(),
            pendingIntent);
而不是

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pendingIntent);
让我知道它是否适用于您

从Android 5.1(API版本22)开始,重复报警的最短时间为1分钟。如果您需要在一分钟内完成工作,只需直接设置警报,然后从该警报的处理程序中设置下一个警报,等等。如果您需要在5秒内完成工作(例如),请将其发送到处理程序,而不是使用AlarmManager

这里的文档很好:


AlarmManager
不适用于如此短的时间间隔。@MikeM。你能告诉我除了计时器以外,我还能用什么短时间间隔吗
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
int ALARM_TYPE = AlarmManager.RTC_WAKEUP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    am.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    am.setExact(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);
else
    am.set(ALARM_TYPE, calendar.getTimeInMillis(), pendingIntent);