geolocation - CheckPermissions Error for LocationManager in Android Studio -
i had working app getgeolocation api level 23( using checkselfpermission). but, had make compatible api level 21. so, checkselfpermission didn't work introduced in api level 23.
so, changed code accomodate this:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); checkforcoarselocationpermission(); } private void checkforcoarselocationpermission() { if (android.os.build.version.sdk_int >= 23) { // android 6.0 , up! boolean accesscoarselocationallowed = false; try { // invoke checkselfpermission method android 6 (api 23 , up) java.lang.reflect.method methodcheckpermission = activity.class.getmethod("checkselfpermission", java.lang.string.class); object resultobj = methodcheckpermission.invoke(this, manifest.permission.access_coarse_location); int result = integer.parseint(resultobj.tostring()); if (result == packagemanager.permission_granted) { accesscoarselocationallowed = true; } } catch (exception ex) { } if (accesscoarselocationallowed) { log.v("tag","granted"); locationmanager locationmanager= (locationmanager)getsystemservice(context.location_service); locationmanager.requestlocationupdates(locationmanager.gps_provider, 0, 0,new locationlistener());//error happening here!!!!!!!!!!! } try { // have invoke method "void requestpermissions (activity activity, string[] permissions, int requestcode) " // android 6 java.lang.reflect.method methodrequestpermission = activity.class.getmethod("requestpermissions", java.lang.string[].class, int.class); methodrequestpermission.invoke(this, new string[] { manifest.permission.access_fine_location }, 0x12345); } catch (exception ex) { } } }
now, locationmanager.requestlocationupdate giving error call requires permission may rejected user: code should explicitly check see if permission available (with checkpermission
) or explicitly handle potential securityexception
less
how tackle it, if want use locationmanager , checkpermission api level >=21.
kindly, help.
try this
if (contextcompat.checkselfpermission(this, manifest.permission.access_coarse_location) == packagemanager.permission_granted) { // coarse permission granted // todo } else { // permission not granted, request permission if (activitycompat.shouldshowrequestpermissionrationale(this, manifest.permission.access_coarse_location)) { // show info user why want permission toast.maketext(this, "allow location permission use functionality.", toast.length_short).show(); activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location, manifest.permission.access_coarse_location}, 123 /*location_permission_request_code*/); } else { activitycompat.requestpermissions(this, new string[]{manifest.permission.access_fine_location, manifest.permission.access_coarse_location}, 123 /*location_permission_request_code*/); } }
note:- must have following permission in android manifest
<uses-permission android:name="android.permission.access_coarse_location" />
Comments
Post a Comment