Android permission for android M doesnt work -


here mapsactivity.java file, have used requestpermission callback , yet dialogbox asking me grant location permission doesnt show , app crashes! package saksham.geofencing;

    @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);         cd = new connectiondetector(getapplicationcontext());         isinternetpresent = cd.isconnectingtointernet();          // check internet status         if (isinternetpresent) {             // internet connection present             // make http requests                     } else {             // internet connection not present             // ask user connect internet             showalertdialog(mapsactivity.this, "no internet connection",                     "you don't have internet connection.");         }         /**          * supportmapfragment belongs v4 support library, contrary default magfragment native component in android.          supportmapfragment support more android versions, additional library have add in project,          think depends on android versions targeting:          •  on recent versions, default components should enough          •  on older versions need install v4 support library , maybe others          *          */         db = openorcreatedatabase("geofence", context.mode_private, null);         db.execsql("create table if not exists coordinates(id integer,latitude double,longitude double,true varchar);");         db.execsql("insert coordinates values(0,0,0,'0')");         db.execsql("insert coordinates values(1,0,0,'0')");         db.execsql("insert coordinates values(2,0,0,'0')");         googlemap = supportmapfragment.getmap();         log.i("my activity", "maps=" + googlemap);         googlemap.setmylocationenabled(true);         /**          * setmylocationenabled(true/false) shows true location when gps switched on device. inbuilt feature of googlemaps .          */          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) {                  mapsactivity.this.requestpermissions(new string[]{manifest.permission.access_fine_location,manifest.permission.write_external_storage,manifest.permission.access_coarse_location}, 100);                  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);           insertgeofence();       }     @override     public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) {         switch (requestcode) {             case 100: {                 if (grantresults[0] == packagemanager.permission_granted) {                     toast.maketext(this, "thanks permission", toast.length_long).show();                     // permission granted, yay!                     // calendar task need do.                 } else {                     // permission denied, boo! disable                     // functionality depends on permission.                     toast.maketext(this, "you did not allow access current location", toast.length_long).show();                 }             }             // other 'switch' lines check other             // permissions app might request         }     }       @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     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) {                  mapsactivity.this.requestpermissions(new string[]{manifest.permission.access_fine_location,manifest.permission.write_external_storage}, 100);                  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) {                 mapsactivity.this.requestpermissions(new string[]{manifest.permission.access_fine_location}, 100);                  return;             }             locationrequest mlocationrequest = new locationrequest();              locationservices.fusedlocationapi.requestlocationupdates(                     mgoogleapiclient, mlocationrequest, this);         }     }      } 

you calling googlemap.setmylocationenabled(true) before checking permissions. also, should instantiate location code after check permissions. also, in onrequestpermissionsresult, may want add in location code want initiated.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -