【问题标题】:Trouble connecting to GooglePlayServices on AVD无法在 AVD 上连接到 GooglePlayServices
【发布时间】:2016-05-09 12:03:11
【问题描述】:

我目前正在尝试访问 GooglePlayServices。 AVD (API 23) 上所有其他功能的互联网工作正常,但我不太确定我的实际应用程序有什么问题。

每次我尝试连接时,它都会说连接失败。我相信我已经正确设置了 GoogleAPIClient,但在 ConnectionRequest 中,我没有自定义任何设置(为简单起见)。

这是我应用的一些代码,用来提供一些信息。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:name=".Protoype2"
    android:allowBackup="true"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SettingsMenu"
        android:label="@string/title_activity_settings_menu"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.denny.protoype2.MainActivity" />
    </activity>
    <activity
        android:name=".ViewingWindow"
        android:label="@string/title_activity_viewing_window"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.denny.protoype2.MainActivity" />
    </activity>
</application>

onCreate();来自相关类

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewing_window);
    mRequestingLocationUpdates = ((Protoype2)getApplication()).getRequestingLocationUpdates();
    if (!mRequestingLocationUpdates) {
        StoppedMessage();
    }
    ExceedInstance = 0;
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .build();
    String[] LocationPermission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    if (ContextCompat.checkSelfPermission(ViewingWindow.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(ViewingWindow.this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {
            GPSExplanation();
        } else {
            ActivityCompat.requestPermissions(ViewingWindow.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }

    }
    onRequestPermissionsResult(MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION, LocationPermission, grantResults);

    final Button BackButton = (Button)findViewById(R.id.VWBackButton);
    BackButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent GoBackIntent = new Intent (ViewingWindow.this, MainActivity.class);
            startActivity(GoBackIntent);
        }
    });
    updateValuesFromBundle(savedInstanceState);

}

最后,几种预定义的连接方式:

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

public void onConnected(Bundle connectionHint) {
    createLocationRequest();
    if (mRequestingLocationUpdates) {
        startLocationUpdates();
    }
}

protected LocationRequest createLocationRequest() {
    return new LocationRequest();
}

【问题讨论】:

    标签: android location google-play-services


    【解决方案1】:

    检查您的 AVD 是否具有适当版本的 Google Play 服务 APK,实现 onConnectionFailed() 回调方法。使用可用于解决错误的参数ConnectionResult,并确定发生了何种错误。

    要解决错误,必须从将非负 requestCode 传递给startResolutionForResult(Activity, int) 的活动开始解决。如果用户已解决问题,应用程序应在其 Activity 中实现 onActivityResult 以再次调用 connect()(resultCode 为 RESULT_OK)。

    基于此documentation,当您的应用收到对onConnectionFailed()) 回调的调用时,您应该在提供的ConnectionResult 对象上调用hasResolution()。如果返回 true,您的应用可以通过在 ConnectionResult 对象上调用 startResolutionForResult() 来请求用户立即采取措施解决错误。

    检查这个相关的question。希望能帮助到你!

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 2018-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 2011-11-18
      相关资源
      最近更新 更多