android - GoogleApiClient is not working in marshmallow and above devices? -
am using googleapi client getting user location working fine below marshmallow devices on marshmallow devices application getting crashed don't know reason can me out let me post code activity trying location:
import android.app.activity; import android.content.context; import android.content.intent; import android.content.intentsender; import android.location.location; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.textview; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.googleapiavailability; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.pendingresult; import com.google.android.gms.common.api.resultcallback; import com.google.android.gms.common.api.status; import com.google.android.gms.gcm.gcmnetworkmanager; import com.google.android.gms.gcm.periodictask; import com.google.android.gms.location.locationlistener; import com.google.android.gms.location.locationrequest; import com.google.android.gms.location.locationservices; import com.google.android.gms.location.locationsettingsrequest; import com.google.android.gms.location.locationsettingsresult; import com.google.android.gms.location.locationsettingsstates; import com.google.android.gms.location.locationsettingsstatuscodes; import java.text.dateformat; import java.util.date; import static precisioninfomatics.backgroundgps.mylocationservice.task_get_location_periodic; public class gps extends activity implements locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, getmethod { private final static int play_services_resolution_request = 9000; private static final string tag = "locationactivity"; private static final long interval = 1000 * 10; private static final long fastest_interval = 1000 * 5; button btnfusedlocation; textview tvlocation; locationrequest mlocationrequest; googleapiclient mgoogleapiclient; location mcurrentlocation; string mlastupdatetime; private asynctaskget asynctaskget; protected static final int request_check_settings = 0x1; protected void createlocationrequest() { mlocationrequest = new locationrequest(); mlocationrequest.setinterval(interval); mlocationrequest.setfastestinterval(fastest_interval); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); log.d(tag, "oncreate ..............................."); if (!checkplayservices()) { finish(); } createlocationrequest(); startservice(new intent(this, gpsservice.class)); mgoogleapiclient = new googleapiclient.builder(this) .addapi(locationservices.api) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .build(); setcontentview(r.layout.activity_gps); tvlocation = (textview) findviewbyid(r.id.tvlocation); btnfusedlocation = (button) findviewbyid(r.id.btnshowlocation); btnfusedlocation.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { if (mcurrentlocation != null) { string lat = string.valueof(mcurrentlocation.getlatitude()); string lng = string.valueof(mcurrentlocation.getlongitude()); string userid = "1"; string time = string.valueof(system.currenttimemillis()); string url = "http://172.16.6.106:8080/gpstracker/api/coordinates/" + lat + "/" + lng + "/" + userid + "/" + time; log.d("url", url); getnotelist(getapplicationcontext()); asynctaskget.execute(url); } } }); locationsettingsrequest.builder builder = new locationsettingsrequest.builder() .addlocationrequest(mlocationrequest); builder.setalwaysshow(true); pendingresult<locationsettingsresult> result = locationservices.settingsapi.checklocationsettings(mgoogleapiclient, builder.build()); result.setresultcallback(new resultcallback<locationsettingsresult>() { @override public void onresult(locationsettingsresult result) { final status status = result.getstatus(); final locationsettingsstates state = result.getlocationsettingsstates(); switch (status.getstatuscode()) { case locationsettingsstatuscodes.success: startlocationupdates(); break; case locationsettingsstatuscodes.resolution_required: // location settings not satisfied. fixed showing user // dialog. try { // show dialog calling startresolutionforresult(), // , check result in onactivityresult(). status.startresolutionforresult( gps.this, request_check_settings); } catch (intentsender.sendintentexception e) { // ignore error. } break; case locationsettingsstatuscodes.settings_change_unavailable: // location settings not satisfied. however, have no way fix // settings won't show dialog. break; } } }); startperiodiclocationtask(); } public void startperiodiclocationtask() { log.d("periodictask", "startperiodiclocationtask"); gcmnetworkmanager mgcmnetworkmanager = gcmnetworkmanager.getinstance(this); periodictask taskbuilder = new periodictask.builder() .setservice(mylocationservice.class) .settag(task_get_location_periodic) .setperiod(30).setflex(20) .setpersisted(true).build(); mgcmnetworkmanager.schedule(taskbuilder); } @override public void onstart() { super.onstart(); log.d(tag, "onstart fired .............."); mgoogleapiclient.connect(); } @override public void onstop() { super.onstop(); log.d(tag, "onstop fired .............."); mgoogleapiclient.disconnect(); log.d(tag, "isconnected ...............: " + mgoogleapiclient.isconnected()); } private boolean checkplayservices() { googleapiavailability googleapi = googleapiavailability.getinstance(); int result = googleapi.isgoogleplayservicesavailable(this); if(result != connectionresult.success) { if(googleapi.isuserresolvableerror(result)) { googleapi.geterrordialog(this, result, play_services_resolution_request).show(); } return false; } return true; } @override public void onconnected(bundle bundle) { log.d(tag, "onconnected - isconnected ...............: " + mgoogleapiclient.isconnected()); startlocationupdates(); } protected void startlocationupdates() { pendingresult<status> pendingresult = locationservices.fusedlocationapi.requestlocationupdates( mgoogleapiclient, mlocationrequest, this); log.d(tag, "location update started ..............: "); } @override public void onconnectionsuspended(int i) { } @override public void onconnectionfailed(connectionresult connectionresult) { log.d(tag, "connection failed: " + connectionresult.tostring()); } @override public void onlocationchanged(location location) { log.d(tag, "firing onlocationchanged.............................................."); mcurrentlocation = location; mlastupdatetime = dateformat.gettimeinstance().format(new date()); updateui(); } private void updateui() { log.d(tag, "ui update initiated ............."); if (null != mcurrentlocation) { string lat = string.valueof(mcurrentlocation.getlatitude()); string lng = string.valueof(mcurrentlocation.getlongitude()); tvlocation.settext("at time: " + mlastupdatetime + "\n" + "latitude: " + lat + "\n" + "longitude: " + lng + "\n" + "accuracy: " + mcurrentlocation.getaccuracy() + "\n" + "provider: " + mcurrentlocation.getprovider()); } else { log.d(tag, "location null ..............."); } } @override protected void onpause() { super.onpause(); stoplocationupdates(); } protected void stoplocationupdates() { locationservices.fusedlocationapi.removelocationupdates( mgoogleapiclient, this); log.d(tag, "location update stopped ......................."); } @override public void onresume() { super.onresume(); if (mgoogleapiclient.isconnected()) { startlocationupdates(); log.d(tag, "location update resumed ....................."); } } public void getnotelist(context context) { asynctaskget = new asynctaskget(context); asynctaskget.getmethod = this; } @override public void getdatafromserver(string objects) { log.d("response", objects); return null; } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { switch (requestcode) { // check integer request code supplied startresolutionforresult(). case request_check_settings: switch (resultcode) { case activity.result_ok: startlocationupdates(); break; case activity.result_canceled: log.d("nogps", "nogps"); break; } break; } } }
my gradle:
dependencies { compile 'com.google.android.gms:play-services:9.8.0' testcompile 'junit:junit:4.12' }
i think making mistake in googleplay service using late googleplay service version can me solve issue!!
runtime permissions issue, in android m , above google added need request permissions when needed (like in ios).
see link: https://developer.android.com/training/permissions/requesting.html
there many wrappers around make adding permissions easier on sites https://android-arsenal.com/tag/235?category=1
here code, taken android developers site along:
// here, thisactivity current activity if (contextcompat.checkselfpermission(thisactivity, manifest.permission.read_contacts) != packagemanager.permission_granted) { // should show explanation? if (activitycompat.shouldshowrequestpermissionrationale(thisactivity, manifest.permission.read_contacts)) { // show expanation user *asynchronously* -- don't block // thread waiting user's response! after user // sees explanation, try again request permission. } else { // no explanation needed, can request permission. activitycompat.requestpermissions(thisactivity, new string[]{manifest.permission.read_contacts}, my_permissions_request_read_contacts); // my_permissions_request_read_contacts // app-defined int constant. callback method gets // result of request. } }
Comments
Post a Comment