当用户离开某个区域时,我会显示警报:
/*Function to show alert when Geofence breached*/
private void showAlert() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Geofence Breached");
builder.setMessage("User has breached the Geofence Boundary!");
builder.setPositiveButton(android.R.string.ok, null);
builder.show();
}
我是这样调用它的:
if( distance[0] > mCircle.getRadius() ){
showAlert();
有什么方法可以设置为每 2 分钟发出一次警报,因为一直在检查位置,然后通知不断出现。我读过 Timers、Timertasks 和 alarmManagers,但我认为它对我不起作用。任何帮助将不胜感激。
最佳答案
在你的 Activity/Service 中有一个成员变量来记录对话框最后显示的时间:
long timeDialogShown = 0;
检查是否显示对话框时,将现在的时间与上次显示对话框的时间进行比较。如果超过 2 分钟,或者以前从未显示过,则显示对话框并更新时间戳。否则什么都不做。
if( distance[0] > mCircle.getRadius() )
{
long timeNow = System.currentTimeMillis()/1000; //Timestamp in seconds
if ( (timeNow - timeDialogShown) > 120 || timeDialogShown == 0) //Show if 2 minutes have passed
{
timeDialogShown = System.currentTimeMillis()/1000; //Timestamp in seconds
showAlert();
}
}
关于android - 设置警报对话框之间的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21992223/