Google place picker is not launching on Android Mi phones-ThrowExceptions

Exception or error:

I am facing a problem with Google Place Picker in Android Redmi Phones version 5.1.1. I am using the following code for launching the Google Place Picker.

try {
       PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
                    startActivityForResult(builder.build(SelectLocationActivity.this), 1);
    } catch (Exception e) {
              e.printStackTrace();
              Toast.makeText(SelectLocationActivity.this,""+e.getMessage(),Toast.LENGTH_LONG).show();
    }

I have added following permissions and api key and google play service version.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application>
<meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="my-api-key" />
</application>

It is working on Every phone except Android Redmi Note2. Any suggestion would be appreciated. Thanks in advance.

How to solve:

Based on your manifest file, I believe that you added the key inside of manifest tag and not application. Based on Android docs, the key should be added like this:

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

Unfortunately, I cannot test it with the device you mentioned, but I remember that I a similar issue with a Samsung device (the code was working on every device, except that one). Also, check the logs again, maybe there is a message and you missed it.

###

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app’s functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app’s Settings screen.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp" >
  ...
  <uses-permission 

android:name=”android.permission.ACCESS_COARSE_LOCATION”/>
android:name=”android.permission.ACCESS_FINE_LOCATION”/>

android:name=”android.permission.ACCESS_COARSE_LOCATION”/>

Add this code to your activity in OnCreate

 askForPermission(Manifest.permission.ACCESS_FINE_LOCATION,LOCATION);

///call this method

 private void askForPermission(String permission, Integer requestCode) {
        if (ContextCompat.checkSelfPermission(MainActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permission)) {

                //This is called if user has denied the permission before
                //In this case I am just asking the permission again
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);

            } else {

                ActivityCompat.requestPermissions(MainActivity.this, new String[]{permission}, requestCode);
            }
        } else {
            Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *