service - Cannot get the getLastLocation method to work in Android Studio -


i testing location services in android , following strictly developer instructions (here :google dev. link) on how this, no luck ever. in layout fine have 2 textviews display latitude , longitude of location object. should mention using genymotion vm gps enabled.

this code in mainactivity:

package criminalintent.android.bignerdranch.com.testgetlocation;  import android.app.activity; import android.location.location; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import android.widget.textview; import android.widget.toast;  import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.drive.drive; import com.google.android.gms.location.locationservices;  public class mainactivity extends activity implements     googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { googleapiclient mgoogleapiclient; private textview mlatitudetext; private textview mlongitudetext;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mlatitudetext=  (textview) findviewbyid(r.id.latitudetextview);     mlongitudetext = (textview) findviewbyid(r.id.longitudetextview);      buildgoogleapiclient();   }  protected synchronized void buildgoogleapiclient() {     toast.maketext(this,"buildgoogleapiclient",toast.length_short).show();      mgoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build(); }  @override public void onconnected(bundle bundle) {     toast.maketext(this,"onconnected",toast.length_short).show();    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 i) {     toast.maketext(this,"onconnectionsuspended",toast.length_short).show(); }  @override public void onconnectionfailed(connectionresult connectionresult) {     toast.maketext(this,"onconnectionfailed",toast.length_short).show(); } } 

i have added permission of getting location services in manifest file:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="criminalintent.android.bignerdranch.com.testgetlocation" >  <uses-permission android:name="android.permission.access_coarse_location"/>  <application     android:allowbackup="true"     android:icon="@mipmap/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> </application> 

this module build.gradle file code:

apply plugin: 'com.android.application'  android { compilesdkversion 22 buildtoolsversion "21.1.2"  defaultconfig {     applicationid "criminalintent.android.bignerdranch.com.testgetlocation"     minsdkversion 14     targetsdkversion 22     versioncode 1     versionname "1.0" } buildtypes {     release {         minifyenabled false         proguardfiles getdefaultproguardfile('proguard-android.txt'),   'proguard-rules.pro'     } } }  dependencies { compile filetree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.google.android.gms:play-services:7.3.0' } 

you missing call connect.

add call connect() in oncreate() after call buildgoogleapiclient():

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mlatitudetext=  (textview) findviewbyid(r.id.latitudetextview);     mlongitudetext = (textview) findviewbyid(r.id.longitudetextview);      buildgoogleapiclient();      mgoogleapiclient.connect(); //add here  } 

note fix, call getlastlocation() have high tendency give null location.

i suggest registering listener if null location, take @ this answer info on how that.

edit: since had code running, went ahead , added location listener. note in code added call removelocationupdates() first onlocationchanged() callback occurs, example:

import android.os.bundle; import android.app.activity; import android.location.location; import android.widget.textview; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.location.locationlistener;  public class mainactivity extends activity implements         googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener {      locationrequest mlocationrequest;     googleapiclient mgoogleapiclient;     private textview mlatitudetext;     private textview mlongitudetext;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mlatitudetext=  (textview) findviewbyid(r.id.latitudetextview);         mlongitudetext = (textview) findviewbyid(r.id.longitudetextview);          buildgoogleapiclient();          mgoogleapiclient.connect(); //add here      }      protected synchronized void buildgoogleapiclient() {         toast.maketext(this,"buildgoogleapiclient",toast.length_short).show();         mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();     }      @override     public void onconnected(bundle bundle) {         toast.maketext(this,"onconnected",toast.length_short).show();         location mlastlocation = locationservices.fusedlocationapi.getlastlocation(                 mgoogleapiclient);         if (mlastlocation != null) {             mlatitudetext.settext(string.valueof(mlastlocation.getlatitude()));             mlongitudetext.settext(string.valueof(mlastlocation.getlongitude()));         }          mlocationrequest = new locationrequest();         mlocationrequest.setinterval(10000); //10 seconds         mlocationrequest.setfastestinterval(5000); //5 seconds         mlocationrequest.setpriority(locationrequest.priority_balanced_power_accuracy);         mlocationrequest.setsmallestdisplacement(1); //1 meter          locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, this);        }      @override     public void onconnectionsuspended(int i) {         toast.maketext(this,"onconnectionsuspended",toast.length_short).show();     }      @override     public void onconnectionfailed(connectionresult connectionresult) {         toast.maketext(this,"onconnectionfailed",toast.length_short).show();     }      @override     public void onlocationchanged(location location) {          toast.maketext(this,"location changed",toast.length_short).show();          mlatitudetext.settext(string.valueof(location.getlatitude()));         mlongitudetext.settext(string.valueof(location.getlongitude()));          //if need 1 location, unregister listener         locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this);      }  } 

also note should add androidmanifest.xml inside application tag:

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

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -