【问题标题】:Android geofence with mock location provider带有模拟位置提供程序的 Android 地理围栏
【发布时间】:2013-06-07 14:09:25
【问题描述】:

我已经开始在 Android 上开发最后一个位置服务功能:地理围栏!!模拟位置提供程序是否存在任何已知问题?以下示例 (https://developer.android.com/training/location/geofencing.html) 即使当前位置在地理围栏内,我的意图服务也从未触发。我正在使用 FakeGPS android 应用程序作为模拟位置提供程序,如果我模拟路线,我会在 Google 地图应用程序上看到位置变化,因此模拟位置提供程序运行良好。有什么想法吗?

谢谢。 保罗。

【问题讨论】:

    标签: android android-location android-geofence


    【解决方案1】:

    我一直在努力让它发挥作用。谷歌多么痛苦!因为它说可以使用模拟轻松测试地理围栏。

    魔术是在传递给 setMockLocation 的位置中使用提供者名称“network”。

        Location location = new Location("network");
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setTime(new Date().getTime());
        location.setAccuracy(3.0f);
        location.setElapsedRealtimeNanos(System.nanoTime());
    
        LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location);
    

    【讨论】:

    • 不是必须先调用 LocationServices.FusedLocationApi.setMockMode(_googleApiClient, true) 吗?
    • 您是否有显示 LocationServices.GeofencingApi 实际上使用 LocationServices.FusedLocationApi 进行模拟的链接?我没有看到任何证据证明这是真的。
    • 天哪!这是一个痛苦的练习。这仍然是相关的。谢谢!
    【解决方案2】:

    实际上,如果您的应用在前台,则在上述示例中使用的 Intent 服务效果很好,但当应用在后台时,该 IntentService 永远不会被调用。因此我们需要使用 Broadcast-Receiver 而不是 Intent 服务。

    我发现这个博客有助于获得解决方案。

    http://davehiren.blogspot.in/2015/01/android-geofence-stop-getting.html

    【讨论】:

    • 欢迎提供解决方案链接,但请确保您的答案在没有它的情况下有用:add context around the link 这样您的其他用户就会知道它是什么以及为什么会出现,然后引用最相关的您链接到的页面的一部分,以防目标页面不可用。 Answers that are little more than a link may be deleted.
    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
    • 除非您对问题及其解决方案有清晰的了解,否则您不应该对答案投反对票。 @宁静
    • 谢谢!! @M.A.R 指出错误,我已经用所需的解决方案编辑了答案。
    【解决方案3】:

    地理围栏使用 FusedLocationProviderApi,因此要模拟它们,您必须使用 FusedLocationProviderApi.setMockLocation

    【讨论】:

    • 此接口已弃用。请改用基于 GoogleApi 的 API FusedLocationProviderClient。
    【解决方案4】:

    确保在您的手机上启用模拟位置。选择设置->开发者选项->“允许模拟位置”。

    【讨论】:

      【解决方案5】:

      LocationServices.FusedLocationApi.setMockMode(googleApiClient, true) 需要在设置 Mock Location 之前使用。

      【讨论】:

        【解决方案6】:

        您可以使用广播接收器代替这样的活动

        public class GeofenceReceiver extends BroadcastReceiver
        implements
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            ResultCallback<Status>{
        
        GoogleApiClient mGoogleApiClient;
        PendingIntent mGeofencePendingIntent ;
        Context mContext;
        
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext = context;
            mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                    .addOnConnectionFailedListener(this)
                    .addConnectionCallbacks(this)
                    .addApi(LocationServices.API)
                    .build();
        
            mGoogleApiClient.connect();
        }
        
        
        
        @Override
        public void onConnected(@Nullable Bundle bundle) {
            try {
                LocationServices.GeofencingApi.addGeofences(
                        mGoogleApiClient,
                        // The GeofenceRequest object.
                        getGeofencingRequest(),
                        getGeofencePendingIntent()
                ).setResultCallback(this); // Result processed in onResult().
            } catch (SecurityException securityException) {
                Log.i(getClass().getSimpleName(),securityException.getMessage());
            }
        }
        
        // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
        @Override
        public void onConnectionSuspended(int i) {
        
        }
        
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        
        }
        
        /**
         * Runs when the result of calling addGeofences() and removeGeofences() becomes available.
         * Either method can complete successfully or with an error.
         *
         * Since this activity implements the {@link ResultCallback} interface, we are required to
         * define this method.
         *
         * @param status The Status returned through a PendingIntent when addGeofences() or
         *               removeGeofences() get called.
         */
        @Override
        public void onResult(@NonNull Status status) {
            if (status.isSuccess()) {
                Log.i(getClass().getSimpleName(),"Success");
            } else {
                // Get the status code for the error and log it using a user-friendly message.
                Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode()));
            }
        }
        
        private GeofencingRequest getGeofencingRequest() {
            GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
            builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_DWELL);
            builder.addGeofences(getGeofecne());
            return builder.build();
        }
        
        private List<Geofence> getGeofecne(){
            List<Geofence> mGeofenceList = new ArrayList<>();
        
            //add one object
            mGeofenceList.add(new Geofence.Builder()
                    // Set the request ID of the geofence. This is a string to identify this
                    // geofence.
                    .setRequestId("key")
        
                    // Set the circular region of this geofence.
                    .setCircularRegion(
                            25.768466, //lat
                            47.567625, //long
                            50) // radios
        
                    // Set the expiration duration of the geofence. This geofence gets automatically
                    // removed after this period of time.
                    //1000 millis  * 60 sec * 5 min
                    .setExpirationDuration(1000 * 60 * 5)
        
                    // Set the transition types of interest. Alerts are only generated for these
                    // transition. We track entry and exit transitions in this sample.
                    .setTransitionTypes(
                            Geofence.GEOFENCE_TRANSITION_DWELL)
                    //it's must to set time in millis with dwell transition
                    .setLoiteringDelay(3000)
                    // Create the geofence.
                    .build());
        
            return mGeofenceList;
        
        }
        
        private PendingIntent getGeofencePendingIntent() {
            // Reuse the PendingIntent if we already have it.
            if (mGeofencePendingIntent != null) {
                return mGeofencePendingIntent;
            }
            Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class);
            return PendingIntent.getService(mContext, 0, intent, PendingIntent.
                    FLAG_UPDATE_CURRENT);
        }
        

        }

        查看我的仓库,有一个使用地理围栏的完整示例 https://github.com/3zcs/Geofence

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-08
          • 1970-01-01
          • 1970-01-01
          • 2011-12-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多