简体   繁体   English

Google Play服务位置API设置问题

[英]Google Play Services Location API setup issue

So I started on android development a few days ago, and am currently facing a problem which I can't seem to sort out. 所以我几天前开始进行Android开发,目前我正面临一个我似乎无法解决的问题。 I am trying to build an app that gives me my latitude and longitude location using the Google Play Services Location API. 我正在尝试使用Google Play服务位置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: 这是我的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. 按钮单击事件调用OnClickUpdate 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". 该函数设置两个TextView元素,以显示API在API能够检索信息时由API确定的纬度和经度,或者将TextView元素设置为包含字符串“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 我使用Google Play Services 9.0.83在运行Marshmallow的Nexus 5和使用Google Play服务的Marshmallow AVD上测试了应用程序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). 我使用Google Play Services 8.4.0在两个设备上构建了应用程序,在build.gradledependencies部分中为app模块添加了一行compile 'com.google.android.gms:play-services:8.4.0' (我只能下载8.4.0,因为8.4.89在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). 然后,我继续使用Google Play Services 9.0.0构建项目,并在build.gradle包含compile 'com.google.android.gms:play-services:9.0.0'build.gradle (再次找不到9.0。 83所以我不得不使用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: 试图在Nexus 5上运行它没有错误,但是当我调用OnClickUpdate ,TextView没有发生任何变化,logcat输出中也存在一些问题:

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: 在AVD上运行相同的版本无法连接到Google Play服务并提供以下消息:

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. 任何帮助将受到高度赞赏。

Encountered errors that you provided can be because you haven't signed in when your Android app attempted connection to the service. 您提供的遇到的错误可能是因为您的Android应用尝试连接到该服务时尚未登录。

It is recommended to try Sign-In for Android . 建议您尝试使用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. 将刚下载的google-services.json文件复制到Android Studio项目的app/mobile/目录中。

If that still doesn't solve the issue, you may try other possible solutions given in this SO post - GoogleService failed to initialize . 如果仍然无法解决问题,您可以尝试在此SO帖子中给出的其他可能解决方案 - GoogleService无法初始化 Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM