5

私は自分のアプリに MapFragment を追加しており、マップのチュートリアルから改作された次のコードがあります。

private boolean servicesConnected() {
    // Check that Google Play services is available
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    // If Google Play services is available
    if (ConnectionResult.SUCCESS == resultCode) {
        // In debug mode, log the status
        Log.d("Location Updates", "Google Play services is available.");
        // Continue
        return true;

    // Google Play services was not available for some reason
    } else {
        GooglePlayServicesUtil.getErrorDialog(resultCode, this, 7).show();
        return false;
    }
}

古いGoogle Playサービスを使用して、工場出荷時の状態にリセットされたGalaxy tab 10.1でテストしています。そのため、MapFragment を開こうとservicesConnected()すると、確認のために呼び出すと、予想どおり、Google Play Services が必要であることを示すダイアログが表示されます。ダイアログの下部に [Google Play サービスを取得] ボタンがありますが、クリックしても何も起こりません。私の LogCat は次の出力を生成します。

07-23 15:30:43.580: W/GooglePlayServicesUtil(2515): Google Play services is missing.
07-23 15:30:48.510: E/SettingsRedirect(2515): Can't redirect to app settings for Google Play services

私は次のonConnectionFailed方法を持っています (基本的には Android 開発者サイトからコピーして貼り付けます):

public void onConnectionFailed(ConnectionResult connectionResult) {
    /*
     * Google Play services can resolve some errors it detects.
     * If the error has a resolution, try sending an Intent to
     * start a Google Play services activity that can resolve
     * error.
     */
    if (connectionResult.hasResolution()) {
        try {
            // Start an Activity that tries to resolve the error
            connectionResult.startResolutionForResult(
                    this,
                    CONNECTION_FAILURE_RESOLUTION_REQUEST);
            /*
             * Thrown if Google Play services canceled the original
             * PendingIntent
             */
        } catch (IntentSender.SendIntentException e) {
            // Log the error
            e.printStackTrace();
        }
    } else {
        /*
         * If no resolution is available, display a dialog to the
         * user with the error.
         */
        GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 7).show();
    }
}

なぜこれが機能しないのですか?どんな助けでも素晴らしいでしょう。

ここに私が取り組んでいる Android dev ページがあります。また、それに関連する SO の投稿もここにあります。

編集

デバイスに Google アカウントが設定されていないことに気付いたので、設定しましたが、違いはありませんでした。

4

2 に答える 2

3

このコードは私のために働いています:

boolean checkGooglePlayServicesAvailable() {
    final int connectionStatusCode = GooglePlayServicesUtil
        .isGooglePlayServicesAvailable(getActivity());
    Log.i(DEBUG_TAG,
    "checkGooglePlayServicesAvailable, connectionStatusCode="
    + connectionStatusCode);
    if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
        showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
        return false;
    }
    Toast t = Toast.makeText(getActivity(),
        "Google Play service available", Toast.LENGTH_LONG);
    t.show();
    return true;
}

void showGooglePlayServicesAvailabilityErrorDialog(
    final int connectionStatusCode) {
    getActivity().runOnUiThread(new Runnable() {
    public void run() {
    final Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
        connectionStatusCode, getActivity(),
        REQUEST_GOOGLE_PLAY_SERVICES);
        if (dialog == null) {
                Log.e(DEBUG_TAG,
                        "couldn't get GooglePlayServicesUtil.getErrorDialog");
                Toast.makeText(getActivity(),
                        "incompatible version of Google Play Services",
                        Toast.LENGTH_LONG).show();
            }
            //this was wrong here -->dialog.show();
       }
    });
}
于 2013-07-29T22:18:57.137 に答える
0

私は同じ問題に遭遇しました。ダイアログはUI スレッドで作成する必要があり、 show()関数も UI スレッドで呼び出す必要があります。

于 2013-09-09T11:16:20.547 に答える