i have copied below code google (don't remember url) runs fine on command click listner event , want know how can use/call same java class (googleplayservicelocation) mainactivity class
package com.dbprox.tagpic; import android.app.activity; import android.content.context; import android.location.location; import android.location.locationlistener; import android.os.bundle; import android.util.log; import android.widget.textview; import android.widget.toast; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleplayservicesutil; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; /** * created user on 24/1/2016. */ public class googleplayservicelocation extends activity implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener,locationlistener{ textview txtviewlat; textview txtviewlon; // private final context context; private final static int play_services_resolution_request=1000; private location mlastlocation; private googleapiclient mgoogleapliclient; private boolean mrequestlocationupdates=false; private locationrequest mlocationrequest; private static int update_interval=10000; private static int fastest_intterval=5000; private static int displacement =10; @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); if(checkplayservices()){ buildgoogleapiclient(); createlocationrequest(); displaylocation(); } } public void googleplayservicelocations (context context) { // this.context=context; if(checkplayservices()){ buildgoogleapiclient(); createlocationrequest(); displaylocation(); } } @override protected void onstart() { super.onstart(); if(mgoogleapliclient!=null) {mgoogleapliclient.connect();} } @override protected void onresume() { super.onresume(); checkplayservices(); if(mgoogleapliclient.isconnected() && mrequestlocationupdates ){ startlocationupdates(); } } @override protected void onstop() { super.onstop(); if(mgoogleapliclient.isconnected()) { mgoogleapliclient.disconnect(); } } @override protected void onpause(){ super.onpause(); stoplocationupdates(); } private void displaylocation(){ mlastlocation= locationservices.fusedlocationapi.getlastlocation(mgoogleapliclient); if(mlastlocation!=null) { double latitude = mlastlocation.getlatitude(); double longitude = mlastlocation.getlongitude(); toast.maketext(getapplicationcontext(), latitude + " - " + longitude , toast.length_long).show(); } else { toast.maketext(getapplicationcontext(),"can'rt connect location", toast.length_long).show(); } } private void toggleperiodlocationupdates(){ if(!mrequestlocationupdates) { mrequestlocationupdates=true; startlocationupdates(); } else { mrequestlocationupdates=false; stoplocationupdates(); } } protected synchronized void buildgoogleapiclient(){ mgoogleapliclient=new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api).build(); } protected void createlocationrequest(){ mlocationrequest=new locationrequest(); mlocationrequest.setinterval(update_interval); mlocationrequest.setfastestinterval(fastest_intterval); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); mlocationrequest.setsmallestdisplacement(displacement); displaylocation(); } private boolean checkplayservices() { int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this); if (resultcode != connectionresult.success) { if (googleplayservicesutil.isuserrecoverableerror(resultcode)) { googleplayservicesutil.geterrordialog(resultcode, this, play_services_resolution_request).show(); } else { toast.maketext(getapplicationcontext(), "this device not supported", toast.length_long).show(); finish(); } return true; } return false; } protected void startlocationupdates(){ locationservices.fusedlocationapi.requestlocationupdates(mgoogleapliclient, mlocationrequest, (com.google.android.gms.location.locationlistener) this); } protected void stoplocationupdates(){ locationservices.fusedlocationapi.removelocationupdates(mgoogleapliclient, (com.google.android.gms.location.locationlistener) this); } @override public void onlocationchanged(location location) { mlastlocation=location; toast.maketext(getapplicationcontext(),"location changed",toast.length_long).show(); displaylocation(); } @override public void onstatuschanged(string provider, int status, bundle extras) { } @override public void onproviderenabled(string provider) { } @override public void onproviderdisabled(string provider) { } @override public void onconnected(bundle bundle) { displaylocation(); if(mrequestlocationupdates){ startlocationupdates(); } } @override public void onconnectionsuspended(int i) { mgoogleapliclient.connect(); } @override public void onconnectionfailed(connectionresult connectionresult) { log.d("tag", "connection failed: " + connectionresult.geterrorcode()); } }
Comments
Post a Comment