App crashes despite handling Android M GPS permission -
i have been using android m model time now, manually revoked permissions location , storage in mobile settings see whether app ask user again permissions @ run time. since then, been crashing. please have @ code-
public class mapsactivity extends fragmentactivity implements com.google.android.gms.location.locationlistener, googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { string[] perms={"android.permission.access_fine_location","android.permission.write_external_storage"}; int permsrequestcode = 200; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); mgeofences = new arraylist<geofence>(); mgeofencecoordinates = new arraylist<latlng>(); // double string0=prefencesettings.getstring("latlng0", "34"); setcontentview(r.layout.activity_maps); showhelpforfirstlaunch(); supportmapfragment supportmapfragment = (supportmapfragment) getsupportfragmentmanager().findfragmentbyid(r.id.googlemap); locationmanager locationmanager = (locationmanager) getsystemservice(service.location_service); // getting gps status boolean isgpsenabled = locationmanager.isproviderenabled(locationmanager.gps_provider); if(!isgpsenabled) { utils.displaypromptforenablinggps(this); } log.i("my activity", "gps is" + isgpsenabled); // getting network status boolean isnetworkenabled = locationmanager .isproviderenabled(locationmanager.network_provider); log.i("my activity", "network is" + isnetworkenabled); criteria crta = new criteria(); if (build.version.sdk_int > build.version_codes.gingerbread) { crta.setaccuracy(criteria.accuracy_fine); } else { crta.setaccuracy(criteria.accuracy_medium); } /** * have used .setaccuracy fine higher sdks gingerbread .gingerbread used reference because in apks lower * gingerbread there poor geo-fencing, gingerbread google made lot easier location services used devleopers. * had improved set of tools location services, included geofencing , substantially improved location discovery. */ crta.setpowerrequirement(criteria.power_low); string provider = locationmanager.getbestprovider(crta, true); /** * request location updates after every 5 sec or if user traveled 10m */ log.i("my activity", "manager " + locationmanager); log.i("my activity", "provider " + provider); if (build.version.sdk_int >= build.version_codes.m) { if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) != packagemanager.permission_granted) { requestpermissions(perms, permsrequestcode); // here request missing permissions, , overriding // public void onrequestpermissionsresult(int requestcode, string[] permissions, // int[] grantresults) // handle case user grants permission. see documentation // activity#requestpermissions more details. return; } } if (mgoogleapiclient == null) { mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks((googleapiclient.connectioncallbacks) this) .addonconnectionfailedlistener((googleapiclient.onconnectionfailedlistener) this) .addapi(locationservices.api) .build(); mgoogleapiclient.connect(); log.i("api is", "" + mgoogleapiclient); } mlocationrequest = new locationrequest(); // want location update every 10 seconds. mlocationrequest.setinterval(10000); // want location accurate possible. mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } public void onrequestpermissionsresult(int permsrequestcode, string[] permissions, int[] grantresults){ switch(permsrequestcode){ case 200: boolean gpsaccepted = grantresults[0]==packagemanager.permission_granted; toast.maketext(this,"thanks permission",toast.length_short).show(); boolean memoryaccepted = grantresults[1]==packagemanager.permission_granted; break; } } @override public void onlocationchanged(location location) { latitude=location.getlatitude(); longitude=location.getlongitude(); cameraposition init = new cameraposition.builder() .target(new latlng(latitude, longitude)) .zoom(17.5f) .bearing(300f) // orientation .tilt(50f) // viewing angle .build(); // use googglemap mmap move camera position googlemap.animatecamera(cameraupdatefactory.newcameraposition(init)); } @override protected void onstart() { super.onstart(); mgoogleapiclient.connect(); } @override protected void onstop() { super.onstop(); mgoogleapiclient.disconnect(); } public void add(view view) { if (request < 3) { string[] arguments = new string[]{string.valueof(request)}; contentvalues values = new contentvalues(); values.put("latitude", latitude); values.put("true","1"); values.put("longitude", longitude); db.update("coordinates", values, "id=?", arguments); mgeofencecoordinates.add(new latlng(latitude, longitude)); log.i("the id is", "" + valueindex); mgeofences.add(new geofence.builder() // coordinates of center of geofence , radius in meters. .setrequestid("" + valueindex) .setcircularregion(latitude, longitude, 100) .setexpirationduration(geofence.never_expire) // required when use transition type of geofence_transition_dwell .setloiteringdelay(50000) .settransitiontypes( geofence.geofence_transition_enter | geofence.geofence_transition_dwell | geofence.geofence_transition_exit).build()); mgeofencestore = new geofencestore(this, mgeofences); valueindex++; request++; googlemap.addmarker(new markeroptions().snippet("radius:100m").draggable(false).title(valueindex + "").position(new latlng(latitude, longitude))).showinfowindow(); } else { toast.maketext(this, "maximum limit exceeded", toast.length_long).show(); } } @override public void onconnected(bundle bundle) { location mlastlocation; if (build.version.sdk_int >= build.version_codes.m) { if (checkselfpermission(manifest.permission.access_fine_location) != packagemanager.permission_granted && checkselfpermission(manifest.permission.access_coarse_location) != packagemanager.permission_granted) { requestpermissions(perms, permsrequestcode); return; } } mlastlocation = locationservices.fusedlocationapi.getlastlocation( mgoogleapiclient); startlocationupdates(); if (mlastlocation != null) { log.i("the last location:", "" + mlastlocation); // toast.maketext(this, "get last location first asshole!", toast.length_long).show(); } } protected void startlocationupdates() { if (build.version.sdk_int >= build.version_codes.m) { if (checkselfpermission(manifest.permission.access_fine_location) != packagemanager.permission_granted && checkselfpermission(manifest.permission.access_coarse_location) != packagemanager.permission_granted) { requestpermissions(perms, permsrequestcode); return; } locationrequest mlocationrequest = new locationrequest(); locationservices.fusedlocationapi.requestlocationupdates( mgoogleapiclient, mlocationrequest, this); } } }
please me out been searching days now! manifest file fine user permission included
your error in line:
string[] perms={"android.permission.access_fine_location","android.permission.write_external_storage"};
change
string[] perms={"android.permission.access_fine_location","android.permission.access_coarse_location"};
Comments
Post a Comment