android - Unable to create service java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission -
i'm working on android tutorial creates odometer service: https://github.com/dogriffiths/headfirstandroid/tree/master/chapter13/odometer
it appears need explicitly ask user permissions use location services. https://developer.android.com/training/permissions/requesting.html
but seems activities extending activitycompat. mainactivity extends activity.
does have example of how implement requestpermissions() when extending activity class? , go...in it's on class or in oncreate?
thanks
contextcompat
public class derived android.support.v4 library , it's not related appcompatactivity. checking permission on android version 23 , this:
private void getpermission(string permission, integer requestcode) { if (contextcompat.checkselfpermission(test_activity.this, permission) != packagemanager.permission_granted) { activitycompat.requestpermissions(test_activity.this, new string[]{permission}, requestcode); } else { toast.maketext(this, "" + permission + " granted.", toast.length_short).show(); } }
than call method in oncreate
:
getpermission(android.manifest.permission.access_fine_location, requestcode);
than override onrequestpermissionsresult
, handle result:
@override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); if(activitycompat.checkselfpermission(this, permissions[0]) == packagemanager.permission_granted){ switch (requestcode) { //location case 1: // break; } else { toast.maketext(this, "permission denied", toast.length_short).show(); } }
Comments
Post a Comment