gpt4 book ai didi

android - 警报管理器未触发

转载 作者:行者123 更新时间:2023-11-30 02:04:54 25 4
gpt4 key购买 nike

在我的代码中,setRepeating() 方法上的 Interval(第三个参数)似乎不是每 5 秒触发一次。

它在时间上不断增加,就像第一个或第二个大多是及时的,但其他的在 40+secs

之后触发

那么这里有什么问题呢?

public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent i = new Intent(this, MainActivity2Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE );
am.setRepeating(RTC_WAKEUP,System.currentTimeMillis(),1000*5,pi);
}

}

最佳答案

看这里:http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int, long, long, android.app.PendingIntent)

当您每 5 秒触发一次警报时:

Note: for timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.

您的代码没问题。您遇到延迟的原因可能是:

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

关于android - 警报管理器未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30832031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com