[英]How to restrict the Android application to run only a certain time period?
[英]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/
上面的教程鏈接看起來很有希望
//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 Beacon庫輕松實現此功能。 遵循有關設置范圍的常規教程。 然后在您的范圍回調中添加如下代碼:
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秒或更長時間時對每個回調執行特殊邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.