我编写了确定我的位置的程序。我仅遵循Google页面thisthis中的说明。
当程序在我的Galaxy note 4(android 5.0.1)上运行时,它会弹出一个窗口来选择我的帐户,然后我选择该帐户,但是之后出现另一个空的弹出窗口并停留在那里,而没有给出我的位置。
但是,当我在弹出窗口之外单击时,弹出窗口消失了,并且我可以正常地看到地图,而没有我的位置。

我只有一堂课,这是:

public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener {

    private GoogleApiClient mGoogleApiClient;
    private static final int REQUEST_RESOLVE_ERROR = 1001;
    private static final String DIALOG_ERROR = "dialog_error";
    private boolean mResolvingError= false;
    private static final String STATE_RESOLVING_ERROR = "resolving_error";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mResolvingError = savedInstanceState != null && savedInstanceState.getBoolean(STATE_RESOLVING_ERROR,false);
    }

    @Override
    protected void onStart() {
        super.onStart();

        if(!mResolvingError)
            mGoogleApiClient.connect();
    };

    @Override
    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    };

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if(mResolvingError)
            return;
        else if(result.hasResolution()){
                 try {
                         mResolvingError = true;
                         result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
                     } catch (SendIntentException e) {
                         mGoogleApiClient.connect();
                     }
             }
             else{
                 showErrorDialog(result.getErrorCode());
                 mResolvingError = true;
             }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

        if(mLastLocation != null){
            //mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
            //mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
        }
    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub
    }

    private void showErrorDialog(int errorCode) {
        ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
        Bundle args = new Bundle();
        args.putInt(DIALOG_ERROR,errorCode);
        dialogFragment.setArguments(args);
        dialogFragment.show(getSupportFragmentManager(),"errordialog");
    }

    public void onDialogDismissed(){
        mResolvingError =false;
    }

    /* A fragment to display an error dialog */
    public static class ErrorDialogFragment extends DialogFragment{

        public ErrorDialogFragment(){};

        public Dialog onCreatDialog(Bundle savedInstanceState){
            int errorCode = this.getArguments().getInt(DIALOG_ERROR);
            return GooglePlayServicesUtil.getErrorDialog(errorCode, this.getActivity(), REQUEST_RESOLVE_ERROR);
        }

        @Override
        public void onDismiss(DialogInterface dialog){
            ((MainActivity)getActivity()).onDialogDismissed();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_RESOLVE_ERROR) {
            mResolvingError = false;

            if (resultCode == RESULT_OK) {
                // Make sure the app is not already connected or attempting to connect
                if (!mGoogleApiClient.isConnecting() &&
                        !mGoogleApiClient.isConnected()) {
                    mGoogleApiClient.connect();
                }
            }
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putBoolean(STATE_RESOLVING_ERROR, mResolvingError);
    }
}


我的清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.secondmapapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="22" />

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

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyAOybgKFHhY0M3Mv_b0RbjT6yrIQCMWDHs"/>
    </application>
</manifest>

最佳答案

我认为这部分是错误的

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();


并且必须更改为

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();


问题是您在哪里指定API类型。

关于java - 按最后发行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30277436/

10-14 03:41