ANDROID PLACE_PICKER (builder.build(context)

狂风中的少年 提交于 2019-12-01 13:53:11

It looks like the main issue is that you're running with an older version of Google Play Services. So in order for it to work for you, you will need to update the version of Google Play Services that is installed on your device or emulator.

This also suggests that you don't have any code that checks if the version required is present on the device, and this is something that you should definitely add, since users in the field may run into the same issue.

The framework provides an easy way to add a prompt to the user to update Google Play Services if the version currently on the device is older than the one required by your app.

Here is a simple example that I just got working.

First, create a member variable status:

int status;

Then, in onCreate() of your Activity, check if Google Play Services are available. If not, then show the dialog that will prompt the user to upgrade:

    status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
            GooglePlayServicesUtil.getErrorDialog(status, this,
                    100).show();
        } 
    }

Then, when you want to show the PlacePicker, ensure that status indicates that Google Play Services are available. You may want to also give some indication to the user (add an else here) if this condition is not met (also note the extra try/catch).

           if (status == ConnectionResult.SUCCESS) {
                int PLACE_PICKER_REQUEST = 199;
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                Context context = this;
                try {
                    startActivityForResult(builder.build(context), PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException e) {
                    e.printStackTrace();
                } catch (GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }

One more thing you could also add would be to re-check the status in onActivityResult() to see if the user has updated Google Play Services. This is also where you would get the result of the PlacePicker dialog:

@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 100){
        status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    }
    if (requestCode == 199){

        //process Intent......
        Place place = PlacePicker.getPlace(data, this);
        String toastMsg = String.format("Place: %s", place.getName());
        Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
    }
}

Result:

For Fragment

           try {
                int PLACE_PICKER_REQUEST = 1;
                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
            }catch (GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException ex){
                ex.printStackTrace();
            }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!