简体   繁体   中英

Android LocationClient always fails to connect

I am trying to implement LocationClient for a personal project, unfortunately, when calling LocationClient's connect() method, it always fails. I tried on the emulator, then on an android device. Concerning the android device, i tried only with wifi, only with data, with wifi and GPS and then with data and GPS. Still fails...

Here is my class :

package com.quentin_cheval.quickpint;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;

import java.util.concurrent.ExecutionException;


public class SearchActivity extends Activity implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener
{

    LocationClient mLocationClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        TextView goTV = (TextView)findViewById(R.id.buttonGo);
        goTV.setClickable(true);

        goTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                mLocationClient = new LocationClient(SearchActivity.this, SearchActivity.this, SearchActivity.this);
                mLocationClient.connect();
               // Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
               // startActivity(intent);
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onConnected(Bundle bundle)
    {
        Log.i("QUICKPINT", "CONNECTED");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult)
    {
        Log.i("QUICKPINT", "Fail");
    }

    @Override
    public void onDisconnected()
    {
        Log.i("QUICKPINT", "DISCONNECTED");
    }
}

Thanks a lot for you help!

check the ConnectionResult connectionResult to see if you can find more info regarding reason for failure. also check logcat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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