仅当在特定时间段内检测到信标时才显示通知
Displaying a notification only when a beacon is detected for a certain period of time
我对andriod应用程序编程很陌生,想问问关于信标的问题。
目前,我正在使用 AltBeacon android-beacon-library 编写应用程序。我喜欢实现一项功能:
理想情况下,我希望在用户靠近特定信标时显示通知。但是,我只希望在用户在该信标周围超过 30 秒时显示通知。如果用户只是走过信标,那么我不希望显示通知。
请问有现成的方法吗?我知道有一种叫做 "startMonitoringBeaconsInRegion" 的方法,但我真的不知道这是否是合适的方法。任何帮助将不胜感激。
使用后台服务通过计时器获取信标可用性。当您获得信标存在时启动计时器,如果信标在该位置可用 30 秒,则在 30 秒后触发通知,否则重置计时器。
信标后台搜索:http://developer.estimote.com/android/tutorial/part-2-background-monitoring/
以上教程 link 看起来很有希望
//start when found beacon in background
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
//send notification
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}, 30000);
//如果信标状态为假或不可用则重置
if (timer != null) {
timer.cancel();
}
您可以通过使用测距的 Android 信标库轻松实现这一点。按照有关设置测距的常规教程进行操作。然后在您的测距回调中添加如下代码:
HashMap<String,Long> beaconDetections = new HashMap<String,Long>();
public void didRangeBeaconsInRegion(Region region, Collection<Beacon> beacons) {
Long now = System.currentTimeMillis();
for (Beacon beacon : beacons) {
Long firstDetectionTime = beaconDetections.get(beacon.toString());
if (firstDetectionTime != null) {
if (now-firstDetectionTime > 30000l) {
// Put logic here for if beacon seen for 30 secs or more
}
}
else {
beaconDetections.put(beacon.toString(), now);
}
}
}
上面的代码使用 HashMap 来跟踪第一次看到每个信标的时间,然后如果它已经出现 30 秒或更长时间,则在每个回调上执行特殊逻辑。
我对andriod应用程序编程很陌生,想问问关于信标的问题。
目前,我正在使用 AltBeacon android-beacon-library 编写应用程序。我喜欢实现一项功能:
理想情况下,我希望在用户靠近特定信标时显示通知。但是,我只希望在用户在该信标周围超过 30 秒时显示通知。如果用户只是走过信标,那么我不希望显示通知。
请问有现成的方法吗?我知道有一种叫做 "startMonitoringBeaconsInRegion" 的方法,但我真的不知道这是否是合适的方法。任何帮助将不胜感激。
使用后台服务通过计时器获取信标可用性。当您获得信标存在时启动计时器,如果信标在该位置可用 30 秒,则在 30 秒后触发通知,否则重置计时器。
信标后台搜索:http://developer.estimote.com/android/tutorial/part-2-background-monitoring/
以上教程 link 看起来很有希望
//start when found beacon in background
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
public void run() {
try {
//send notification
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}, 30000);
//如果信标状态为假或不可用则重置
if (timer != null) {
timer.cancel();
}
您可以通过使用测距的 Android 信标库轻松实现这一点。按照有关设置测距的常规教程进行操作。然后在您的测距回调中添加如下代码:
HashMap<String,Long> beaconDetections = new HashMap<String,Long>();
public void didRangeBeaconsInRegion(Region region, Collection<Beacon> beacons) {
Long now = System.currentTimeMillis();
for (Beacon beacon : beacons) {
Long firstDetectionTime = beaconDetections.get(beacon.toString());
if (firstDetectionTime != null) {
if (now-firstDetectionTime > 30000l) {
// Put logic here for if beacon seen for 30 secs or more
}
}
else {
beaconDetections.put(beacon.toString(), now);
}
}
}
上面的代码使用 HashMap 来跟踪第一次看到每个信标的时间,然后如果它已经出现 30 秒或更长时间,则在每个回调上执行特殊逻辑。