2015-02-05 5 views
9

तो, मैं GoogleApiClient का उपयोग करके अपना अंतिम स्थान प्राप्त करने का प्रयास कर रहा हूं। मैं एक कनेक्ट स्टेटमेंट निष्पादित कर रहा हूं, लेकिन कनेक्ट होने पर फायरिंग नहीं है, न ही कनेक्शन पर फेलिंग है। कोई विचार?GoogleApiClient: कनेक्ट नहीं हो रहा है या कनेक्शन पर फेल नहीं किया गया

टिप्पणी: मैं इसे एक एमुलेटर पर चला रहा हूं, असली डिवाइस नहीं। एम्यूलेटर काम पर ब्राउज़िंग।

package com.example.jdc.testjdc; 

import android.location.Location; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import com.google.android.gms.common.api.*; 
import com.google.android.gms.drive.*; 
import com.google.android.gms.location.LocationServices; 
import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 



import android.support.v4.app.FragmentActivity; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener { 
    private GoogleApiClient mGoogleApiClient; 
    private Location mLastLocation; 
    private TextView mLatitudeText; 
    private TextView mLongitudeText; 
    private TextView Toast; 

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

    } 
    // public GoogleApiClient mGoogleApiClient; 
    protected synchronized void buildGoogleApiClient() { 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addApi(LocationServices.API) 
       .build(); 
     // mGoogleApiClient.connect(); 
    } 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult){ 
     Log.e("Connected failed", String.valueOf(mGoogleApiClient.isConnected())); 
    } 

    public void onStart(){ 
     super.onStart(); 
     Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected())); 
     mGoogleApiClient.connect(); 
     Log.e("Connected?", String.valueOf(mGoogleApiClient.isConnected())); 
    } 
    public void onConnected(Bundle connectionHint) { 

     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude())); 
      mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude())); 
      TextView t=new TextView(this); 

      t=(TextView)findViewById(R.id.mLatitude); 
      t.setText(String.valueOf(mLastLocation.getLatitude())); 
      setContentView(mLatitudeText); 
     } 
    } 



    @Override 
    public void onConnectionSuspended(int i) { 

    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, 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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

और यहाँ मैनिफ़ेस्ट फ़ाइल

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.jdc.testjdc" > 
    package="com.google.android.gms.location.sample.basiclocationsample" > 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <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" /> 
    </application> 

</manifest> 

मैं यहाँ नुकसान में हूँ है। डीबगर के माध्यम से मैंने पाया है कि उपर्युक्त कार्यों को कभी भी निकाल दिया नहीं जाता है ...

उत्तर

7

आपको अपनी विधि buildGoogleApiClient() में कॉलबैक पंजीकृत करने की आवश्यकता है।

+0

धन्यवाद अपने ग्राहक को श्रोताओं को जोड़ने की जरूरत है, कि काम करता है बदलने के लिए! –

+1

@JohanDeCoster कृपया उत्तर स्वीकार करना याद रखें, StackOverflow एक साधारण मंच नहीं है – greywolf82

9

कोशिश अपने buildGoogleApiClient() विधि

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

आप सभी यह सिर्फ

   .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
+0

मुझे एक ही समस्या का सामना करना पड़ रहा है। परिवर्तन के साथ क्या हो रहा है कृपया बताएं। – Pravin

+0

हम इस क्रियाओं के लिए googleApiClient listners पर सेट हैं .addConnectionCallbacks (यह) .addOnConnectionFailedListener (यह), तो googleApiClient कनेक्ट करने के बाद आपके श्रोताओं को कॉलबैक देता है (addConnectionCallbacks, addOnConnectionFailedListener) आपकी कक्षा में –

संबंधित मुद्दे