【问题标题】:When is OnResult called in an application ? (ResultCallback)什么时候在应用程序中调用 OnResult? (结果回调)
【发布时间】:2016-11-07 16:20:51
【问题描述】:

我从 google places apis 开始,我正在尝试获取设备的当前位置,这是我到目前为止所做的,但我不知道如何以及何时调用 OnResult 中的代码,因为它里面的日志不会打印在 logcat 中:

Log.d("2^^check1111","nside : beforee");

    try {
        if (ActivityCompat.checkSelfPermission(getActivity().getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Log.d("2here now","yoyo0");
            ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 123);
        }

        Log.d("999here now","yoyo0");

        PendingResult<PlaceLikelihoodBuffer> result =  Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);

        Log.d("888here now","after result");

        result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {

            @Override
            public void onResult(@NonNull PlaceLikelihoodBuffer likelyPlaces) {
             //    utils.showLog(TAG, "" + likelyPlaces.getCount());

               /// Toast.makeText(getActivity().getApplicationContext(),"inside : onResult",Toast.LENGTH_LONG).show();
                Log.d("2^^check","nside : onResul");
                if (likelyPlaces.getCount() > 0) {


                //    Toast.makeText(getActivity().getApplicationContext(),"inside :likely >0",Toast.LENGTH_LONG).show();
                    Log.d("2^^check2","nside :likely >0");
                    PlaceLikelihood placeLikelihood = likelyPlaces.get(0);
                    //                String content = "";
                    if (placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty(placeLikelihood.getPlace().getName())) {

                        Log.d("2^^check3","nside : placelikelihood not nu");
                    //    Toast.makeText(getActivity().getApplicationContext(),"inside : placelikelihood not null",Toast.LENGTH_LONG).show();
   LatLng FromLatLng = placeLikelihood.getPlace().getLatLng();
                        Log.d("2^^check4","4444"+FromLatLng);
                     //   Toast.makeText(getActivity().getApplicationContext(),"inside : latlng :"+FromLatLng,Toast.LENGTH_LONG).show();
                        newLatLng = FromLatLng;
                        FromLat = FromLatLng.latitude;
                        FromLng = FromLatLng.longitude;
                    }
                } else {

       Toast.makeText(getActivity(), "Auto Location can't fetch", Toast.LENGTH_SHORT).show();
                }
                likelyPlaces.release();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 您的代码位于 try-catch 块中。在 catch 中放置一些日志或断点,看看你的代码是否抛出了一些异常。
  • 尝试在 catch 块中添加日志,没有发现异常

标签: android google-api google-play-services google-places-api android-pendingintent


【解决方案1】:

您需要注册一个 API 密钥,否则结果将不会显示。

在此注册:https://developers.google.com/places/android-api/signup

然后在您的AndroidManifest.xml 中包含:

<application>
  ...
  <meta-data
      android:name="com.google.android.geo.API_KEY"
      android:value="YOUR_API_KEY"/>
</application>

编辑: 所以我将你的代码添加到一个新项目中,发现 onResult() 从未执行过。但是,经过一些更改后,我能够使其正常工作。

首先,我将我的活动设为FragmentActivity

然后我像这样使用GoogleApiClient.Builder():

final GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
    .addApi(Places.PLACE_DETECTION_API)
    .addApi(Places.GEO_DATA_API)
    .enableAutoManage(this, this)
    .build();

执行此操作后,我看到 onResult() 现在正在被执行。

【讨论】:

  • 非常感谢您抽出宝贵的时间,我发现问题是我从未添加代码来连接 GoogleApiClient,即 .enableAutoManage(this, this)
【解决方案2】:

您需要在您的AndroidManifest.xml 中设置ACCESS_FINE_LOCATION 权限。如果未设置,则位置数据将不可用,并且永远不会发布结果。

【讨论】:

  • 但是我已经在 manifest 中添加了这个权限,还有 meta data 中的 geo.api 键,manifest 中有 readgservices 权限。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 2014-03-27
  • 2014-09-06
  • 1970-01-01
相关资源
最近更新 更多