[英]Android Location Based Reminder / Event?
我正在尋找關於當用戶設備接近預定義位置時如何獲取事件的代碼示例。 我在這里看到如何使用Google Places API獲取自動填寫的地點列表:
https://developers.google.com/places/training/autocomplete-android
使用基於上述的位置,當用戶接近他們選擇的位置時,如何通知我的應用。 如果我創建的服務不斷輪詢位置,那么Google PLaces API的每日使用量將超過分配的金額(您每天只能獲得1,000個請求)...
使用Google Play服務,您需要先設置Google API客戶端
private void setUpApi() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
當客戶端連接時,您可以使用您已知的坐標設置提醒:
@Override
public void onConnected(Bundle connectionHint) {
Log.d(TAG, "connected!!");
Geofence cinemaFence = new Geofence.Builder()
.setRequestId(id) // some id for you
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setCircularRegion(mLatitude, mLongtitude, mRadius)
.build();
GeofencingRequest request = new GeofencingRequest.Builder()
.addGeofence(cinemaFence)
.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
.build();
// Now we create an intent that will be fired when the user enters the region
Intent intent = new Intent(mContext, UserTransitionIntentService.class);
intent.putExtra(UserTransitionIntentService.EXTRA_FEATURES, getFeaturesFlag());
PendingIntent pi = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, request, pi);
}
完整的項目可以在這里找到: https : //github.com/alexstyl/Location-Reminder
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.