简体   繁体   English

基于Android位置的提醒/事件?

[英]Android Location Based Reminder / Event?

I'm looking for code samples on how to get an event when a users device is close to a pre defined location. 我正在寻找关于当用户设备接近预定义位置时如何获取事件的代码示例。 I see here how to get an auto completed list of locations using the Google Places API here: 我在这里看到如何使用Google Places API获取自动填写的地点列表:

https://developers.google.com/places/training/autocomplete-android https://developers.google.com/places/training/autocomplete-android

Using a location based on above, how can my app be notified when the user is close to the location they select. 使用基于上述的位置,当用户接近他们选择的位置时,如何通知我的应用。 If I create a service that constantly polls location, then the daily usage for Google PLaces API will be way over the allotted amount (you only get 1,000 requests/day)... 如果我创建的服务不断轮询位置,那么Google PLaces API的每日使用量将超过分配的金额(您每天只能获得1,000个请求)...

Using the Google Play Services, you need to set up the Google API Client first 使用Google Play服务,您需要先设置Google API客户端

private void setUpApi() {
        mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        mGoogleApiClient.connect();
    }

when the client is connected you can set up your reminder, using the coords you already know: 当客户端连接时,您可以使用您已知的坐标设置提醒:

@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);
    }

The full project can be found here: https://github.com/alexstyl/Location-Reminder 完整的项目可以在这里找到: https//github.com/alexstyl/Location-Reminder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM