1

So I started on android development a few days ago, and am currently facing a problem which I can't seem to sort out. I am trying to build an app that gives me my latitude and longitude location using the Google Play Services Location API. I saw some tutorials online and also saw the google developers training page and built an app to get my location. The app however doesn't respond to anything I do.

Here's my MainActivity:

package com.example.chris.locationfinder;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "Error";
    public GoogleApiClient GPlayAPI;
    public Location mLastLocation;
    private TextView mLatitudeText;
    private TextView mLongitudeText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLatitudeText = (TextView) findViewById(R.id.lati);
        mLongitudeText =(TextView) findViewById(R.id.longi);
        // ATTENTION: This "addApi(AppIndex.API)"was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        GPlayAPI = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .addApi(AppIndex.API).build();
    }

    protected void onStart() {
        GPlayAPI.connect();
        super.onStart();

    }

    protected void onStop() {
        GPlayAPI.disconnect();
        super.onStop();

    }

    public void OnClickUpdate(View view) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
            return;
        }

        if (mLastLocation==null){

            mLatitudeText.setText("NULL");
            mLongitudeText.setText("NULL");

        }
        else {

            mLatitudeText.setText(Double.toString(mLastLocation.getLatitude()));
            mLongitudeText.setText(Double.toString(mLastLocation.getLongitude()));
        }
    }


    @Override
    public void onConnected(Bundle connectionHint) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_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.
            return;
        }

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                GPlayAPI);
    }

    @Override
    public void onConnectionSuspended(int i) {

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
                + result.getErrorCode());
    }
}

OnClickUpdate is called on by a button click event. The function sets two TextView elements to display the latitude and longitude determined by the API when the API is able to retrieve the information or else it sets the TextView elements to contain the string "NULL".

I tested the app on my Nexus 5 running Marshmallow with Google Play Services 9.0.83 and on a Marshmallow AVD with Google Play Services 8.4.89

I built the apps on both devices using Google Play Services 8.4.0 by including the line compile 'com.google.android.gms:play-services:8.4.0' in the dependencies section of the build.gradle for the app module (I could only download 8.4.0 since 8.4.89 isn't available on android studio). On both the devices launching the application resulted in the following error:

GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.

I then went on to build the project with Google Play Services 9.0.0 and included the line compile 'com.google.android.gms:play-services:9.0.0' in the build.gradle (Again I could not find 9.0.83 so I had to use 9.0.0). Trying to run this on the Nexus 5 gives no Errors but no change in the TextView's took place when I called OnClickUpdate , there are also some issues in the logcat output:

05-27 22:15:15.646 21349-21349/com.example.chris.locationfinder W/System: ClassLoader referenced unknown path: /data/app/com.example.chris.locationfinder-2/lib/arm
05-27 22:15:30.095 21349-21349/com.example.chris.locationfinder W/System: ClassLoader referenced unknown path: /data/app/com.example.chris.locationfinder-2/lib/arm
05-27 22:15:30.136 21349-21349/com.example.chris.locationfinder I/FirebaseInitProvider: FirebaseApp initialization unsuccessful
05-27 22:15:30.597 21349-21349/com.example.chris.locationfinder W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-27 22:15:30.923 21349-22261/com.example.chris.locationfinder V/GoogleSignatureVerifier: com.google.android.gms signature not valid.  Found: 
                                                                                           MIIEQzCCAyugAwIBAgIJAMLgh0ZkSjCNMA0GCSqGSIb3DQEBBAUAMHQxCzAJBgNVBAYTAlVTMRMw
                                                                                           EQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtHb29n
                                                                                           bGUgSW5jLjEQMA4GA1UECxMHQW5kcm9pZDEQMA4GA1UEAxMHQW5kcm9pZDAeFw0wODA4MjEyMzEz
                                                                                           MzRaFw0zNjAxMDcyMzEzMzRaMHQxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYw
                                                                                           FAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtHb29nbGUgSW5jLjEQMA4GA1UECxMHQW5k
                                                                                           cm9pZDEQMA4GA1UEAxMHQW5kcm9pZDCCASAwDQYJKoZIhvcNAQEBBQADggENADCCAQgCggEBAKtW
                                                                                           LgDYO6IIrgqWbxJOKdoR8qtW0I9Y4sypEwPpt1TTcvZApxsdyxMJZ2JORland2qSGT2y5b+3JKke
                                                                                           dxiLDmpHpDsz2WCbdxgxRczfey5YZnTJ4VZbH0xqWVW/8lGmPav5xVwnIiJS6HXk+BVKZF+JcWjA
                                                                                           sb/GEuq/eFdpuzSqeYTcfi6idkyugwfYwXFU1+5fZKUaRKYCwkkFQVfcAs1fXA5V+++FGfvjJ/Cx
                                                                                           URaSxaBvGdGDhfXE28LWuT9ozCl5xw4Yq5OGazvV24mZVSoOO0yZ31j7kYvtwYK6NeADwbSxDdJE
                                                                                           qO4k//0zOHKrUiGYXtqw/A0LFFtqoZKFjnkCAQOjgdkwgdYwHQYDVR0OBBYEFMd9jMIhF1Ylmn/T
                                                                                           gt9r45jk14alMIGmBgNVHSMEgZ4wgZuAFMd9jMIhF1Ylmn/Tgt9r45jk14aloXikdjB0MQswCQYD
                                                                                           VQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIG
                                                                                           A1UEChMLR29vZ2xlIEluYy4xEDAOBgNVBAsTB0FuZHJvaWQxEDAOBgNVBAMTB0FuZHJvaWSCCQDC
                                                                                           4IdGZEowjTAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBAUAA4IBAQBt0lLO74UwLDYKqs6Tm8/y
                                                                                           zKkEu116FmH4rkaymUIE0P9KaMftGlMexFlaYjzmB2OxZyl6euNXEsQH8gjwyxCUKRJNexBiGcCE
                                                                                           yj6z+a1fuHHvkiaai+KL8W1EyNmgjmyy8AW7P+LLlkR+ho5zEHatRbM/YAnqGcFh5iZBqpknHf1S
                                                                                           KMXFh4dd239FJ1jWYfbMDMy3NS5CTMQ2XFI1MvcyUTdZPErjQfTbQe3aDQsQcafEQPD+nqActifK
                                                                                           Z0Np0IS9L9kR/wbNvyz6ENwPiTrjV2KRkEjH78ZMcUQXg0L3BYHJ3lc69Vs5Ddf9uUGGMYldX3Wf
                                                                                           MBEmh/9iFBDAaTCK
05-27 22:15:30.933 21349-22262/com.example.chris.locationfinder D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-27 22:15:31.038 21349-22262/com.example.chris.locationfinder I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
05-27 22:15:31.042 21349-22262/com.example.chris.locationfinder I/OpenGLRenderer: Initialized EGL, version 1.4

Running the same build on the AVD failed to connect to Google Play Services and gave the following message:

05-27 22:32:43.161 4983-4983/com.example.chris.locationfinder I/Error: Connection failed: ConnectionResult.getErrorCode() = 2

                                                                       [ 05-27 22:32:43.196  4983: 5325 D/         ]
                                                                       HostConnection::get() New Host Connection established 0xaa102e10, tid 5325
05-27 22:32:43.211 4983-5325/com.example.chris.locationfinder I/OpenGLRenderer: Initialized EGL, version 1.4

I don't know what I'm doing wrong. Any help would be highly appreciated.

1 Answers1

0

Encountered errors that you provided can be because you haven't signed in when your Android app attempted connection to the service.

It is recommended to try Sign-In for Android. You need to get the configuration file from the given link. Then, add the configuration file to your project. As stated in the documentation:

Copy the google-services.json file you just downloaded into the app/ or mobile/ directory of your Android Studio project.

If that still doesn't solve the issue, you may try other possible solutions given in this SO post - GoogleService failed to initialize. Hope that helps.

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22