所以我几天前开始进行Android开发,目前我正面临着一个我似乎无法解决的问题。我正在尝试使用Google Play服务位置API构建一个可以为我提供纬度和经度位置的应用。我在网上看到了一些教程,还看到了谷歌开发人员培训页面,并构建了一个应用程序来获取我的位置。然而,该应用程序不响应我做的任何事情。
这是我的主要活动:
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
。该函数设置两个TextView元素,以便在API能够检索信息时显示由API确定的纬度和经度,或者将TextView元素设置为包含字符串“NULL”。
我在使用Google Play Services 9.0.83运行Marshmallow的Nexus 5和使用Google Play服务的Marshmallow AVD上测试了应用程序8.4.89
我使用Google Play Services 8.4.0在两台设备上构建应用程序,在compile 'com.google.android.gms:play-services:8.4.0'
部分的dependencies
部分添加了build.gradle
部分用于应用程序模块(我只能下载8.4.0因为8.4.89在android studio上不可用)。在启动应用程序的两个设备上都会导致以下错误:
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.
然后我继续使用Google Play Services 9.0.0构建项目,并在compile 'com.google.android.gms:play-services:9.0.0'
中添加了行build.gradle
(再次找不到9.0.83,所以我不得不使用9.0。 0)。
试图在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
LgDYO6IIrgqWbxJOKdoR8qtW0I9Y4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-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
在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
我不知道我做错了什么。任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
您提供的遇到的错误可能是因为您的Android应用尝试连接到该服务时尚未登录。
建议try Sign-In for Android。您需要从给定链接获取配置文件。然后,将配置文件添加到项目中。如文档中所述:
将刚刚下载的
google-services.json
文件复制到Android Studio项目的app/
或mobile/
目录中。
如果仍然无法解决问题,您可以尝试此SO帖子中提供的其他可能解决方案 - GoogleService failed to initialize。希望有所帮助。