xml - Android: Getting device location during runtime -
i'm developing app want lat,lng of user's device.i found need access_fine_location permission access_coarse_location, know should ask them during runtime including them in androidmanifest.xml, app asks me permission access_coarse_location, , ignores asking access_fine_location. here's code
private boolean checkandrequestpermissions(){ int permissionreadcontacts = contextcompat.checkselfpermission(this, manifest.permission.read_contacts); int locationpermission = contextcompat.checkselfpermission(this, manifest.permission.access_coarse_location); int locationpermission2 =contextcompat.checkselfpermission(this, manifest.permission.access_fine_location); list<string> listpermissionsneeded = new arraylist<string>(); if (locationpermission != packagemanager.permission_granted) { listpermissionsneeded.add(manifest.permission.access_coarse_location); } if (locationpermission2 != packagemanager.permission_granted) { log.i("addingperm","addingperm"); listpermissionsneeded.add(manifest.permission.access_fine_location); } if (permissionreadcontacts != packagemanager.permission_granted) { listpermissionsneeded.add(manifest.permission.read_contacts); } if (!listpermissionsneeded.isempty()) { activitycompat.requestpermissions(this, listpermissionsneeded.toarray(new string[listpermissionsneeded.size()]),request_id_multiple_permissions); return false; } return true; }
and here's callback:
@override public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) { string tag = "sonder.splashactivity"; log.d(tag, "permission callback called-------"); switch (requestcode) { case request_id_multiple_permissions: { map<string, integer> perms = new hashmap<string, integer>(); // initialize map both permissions perms.put(manifest.permission.access_coarse_location, packagemanager.permission_granted); perms.put(manifest.permission.access_fine_location, packagemanager.permission_granted); perms.put(manifest.permission.read_contacts, packagemanager.permission_granted); // fill actual results user if (grantresults.length > 0) { (int = 0; < permissions.length; i++) perms.put(permissions[i], grantresults[i]); // check both permissions if (activitycompat.checkselfpermission(this, manifest.permission.access_fine_location) == packagemanager.permission_granted && activitycompat.checkselfpermission(this, manifest.permission.access_coarse_location) == packagemanager.permission_granted) { locationmanager locationmanager = (locationmanager) getsystemservice(context.location_service); locationlistener locationlistener = new loclistener(); locationmanager.requestlocationupdates(locationmanager.gps_provider, 5000, 10, locationlistener); userdata.setlocation(loclistener.getlat() + "," + loclistener.getlon()); log.i("aaaaaaaaaa", "aaaaaaaaaa"); log.i("" + loclistener.getlat(), "" + loclistener.getlon()); if (perms.get(manifest.permission.access_fine_location) == packagemanager.permission_granted) { log.i("ahmed", "ahmed"); log.d("permissions", "location services permission granted"); facebooksdk.sdkinitialize(getapplicationcontext()); thread mythread = new thread() { @override public void run() { try { log.i("may", "may"); sleep(300); intent intent = new intent(getapplicationcontext(), setup_activity.class); startactivity(intent); finish(); } catch (interruptedexception e) { e.printstacktrace(); } } }; mythread.start(); } else { //if permissions denied log.d(tag, "location permission not granted ask again"); intent intent = new intent(getapplicationcontext(), setup_activity.class); startactivity(intent); finish(); } } } } } }
and here's androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.authenticate_accounts" /> <uses-permission android:name="android.permission.get_accounts" /> <uses-permission android:name="android.permission.use_credentials" /> <uses-permission android:name="android.permission.manage_accounts" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permissions.network_access" /> <uses-permission android:name="android.permission.read_contacts" /> <uses-permission android:name="android.permissions.access_network_state" /> <uses-permission android:name="android.permissions.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-permission android:name="android.permission.access_location_extra_commands"/> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-feature android:name="android.hardware.location.gps" android:required="true"/> <uses-feature android:name="android.hardware.location.network" android:required="true"/> <uses-feature android:name="android.hardware.location" android:required="true" /> <application android:name=".utils.orientation_adj" android:allowbackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:roundicon="@drawable/icon" android:theme="@style/apptheme" android:largeheap="true"> <activity android:name=".home.splashscreen" android:theme="@style/apptheme.noactionbar"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".home.main_activity" android:theme="@style/apptheme.noactionbar"> </activity> <activity android:name=".drawer.drawer_preferences"></activity> <activity android:name=".setup.edit_profile_tags" android:theme="@style/dialognotitle"></activity> <activity android:name=".setup.setup_activity" android:theme="@style/apptheme"> </activity> <activity android:name=".setup.backend_test"></activity> <activity android:name=".drawer.drawer_view_profile" android:theme="@style/apptheme.noactionbar"></activity> <activity android:name=".drawer.drawer_settings" android:theme="@style/switch"></activity> <activity android:name=".drawer.drawer_help"></activity> <activity android:name=".setup.not_available_in_your_country" android:theme="@style/apptheme.noactionbar"> <!--add intent filter here--> </activity> <activity android:name=".setup.readiness" android:theme="@style/apptheme.noactionbar"> </activity> <activity android:name=".setup.locationpermission" android:theme="@style/apptheme.noactionbar"> </activity> <service android:name=".utils.genericaccountservice" android:exported="false"> <intent-filter> <action android:name="android.accounts.accountauthenticator" /> </intent-filter> <meta-data android:name="android.accounts.accountauthenticator" android:resource="@xml/authenticator" /> </service> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/facebook_app_id" /> <meta-data android:name="com.google.android.geo.api_key" android:value="@string/google_maps_key" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="io.fabric.apikey" android:value="43a4edcd41e180a4beca5aae4040115cfc1b52fe" /> </application>
three of <uses-permission>
elements have incorrect permission names. there no android sdk permissions begin android.permissions
(note s
on end). so, example, need change android.permissions.access_fine_location
android.permission.access_fine_location
. cannot request runtime permission if <uses-permission>
element missing or invalid.
Comments
Post a Comment