HttpUrlConnection request error (Missing Internet permission when the permission is enabled) - android

I'm having the following error on a few of my users:
Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
at [my communication HttpUrlConnection request]
... 12 more
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:61)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
... 25 more
Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
... 28 more
The app requires internet to do it's most basic function, I don't think a user have removed the permission using root or something like that.
Additionally, this error was sent to me with my own error reporting tool. I have made the upload with the registered error and at that moment the permission was available.
There is no restriction on device model of android version that has the error.
Example of a part of the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
[...]
Any clue is appreciated,
Thanks in advance.

This will help you.
Write the following in your onCreate method
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.CAMERA};
if (!hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
And get this method
public static boolean hasPermissions(Context context, String... permissions)
{
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null)
{
for (String permission : permissions)
{
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED)
{
return false;
}
}
}
return true;
}
The application will ask for only those permission which the application needs, that too only once. If it works upvote and do let me know. It worked for me

HttpUrlConnection request also requires android.permission.ACCESS_NETWORK_STATE. Do you also have this permission listed in the manifest?
Please post your manifest file so we can see that android.permission.ACCESS_NETWORK_STATE, and android.permission.INTERNET are both listed without any spelling, capitalization, syntax errors.
The issue can also happen if a user has rooted their Android device and tinkered with the permissions. How do you know the user has not rooted the device? Can you reproduce the problem on any of your own devices?
See more detail on this issue here: SecurityException: Permission denied (missing INTERNET permission?)

Related

java.net.BindException: bind failed: EACCES (Permission denied) while using sockets [duplicate]

I have seen several questions regarding SocketException when using Android, but none of them cover the BindException that I get even with the INTERNET permission specified in my manifest.
Here is part of my manifest:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_OWNER_DATA"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"></uses-permission>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"></uses-permission>
Here is the relevant portion of my LogCat output:
04-22 14:49:06.117: DEBUG/MyLibrary(4844): Address to bind: 192.168.1.14 port: 843
04-22 14:49:06.197: WARN/System.err(4844): java.net.BindException: Permission denied (maybe missing INTERNET permission)
04-22 14:49:06.207: WARN/System.err(4844): at org.apache.harmony.luni.platform.OSNetworkSystem.socketBindImpl(Native Method)
04-22 14:49:06.207: WARN/System.err(4844): at org.apache.harmony.luni.platform.OSNetworkSystem.bind(OSNetworkSystem.java:107)
04-22 14:49:06.217: WARN/System.err(4844): at org.apache.harmony.luni.net.PlainSocketImpl.bind(PlainSocketImpl.java:184)
04-22 14:49:06.217: WARN/System.err(4844): at java.net.ServerSocket.bind(ServerSocket.java:414)
04-22 14:49:06.227: WARN/System.err(4844): at org.apache.harmony.nio.internal.ServerSocketChannelImpl$ServerSocketAdapter.bind(ServerSocketChannelImpl.java:213)
04-22 14:49:06.227: WARN/System.err(4844): at java.net.ServerSocket.bind(ServerSocket.java:367)
04-22 14:49:06.237: WARN/System.err(4844): at org.apache.harmony.nio.internal.ServerSocketChannelImpl$ServerSocketAdapter.bind(ServerSocketChannelImpl.java:283)
04-22 14:49:06.237: WARN/System.err(4844): at mylibrary.net.PolicyConnection$PolicyServerWorker.(PolicyConnection.java:201)
I Really hope this is a simple problem and not something complicated by the fact that the binding is occurring within a worker thread on a port less than 1024.
Update
Looks as if this is a privileged port issue, anyone know how to bind to ports lower than 1024 in Android?
SelectorProvider provider = SelectorProvider.provider();
try {
ServerSocketChannel channel = provider.openServerSocketChannel();
policySocket = channel.socket();
Log.d("MyLibrary", "Address to bind: " + device.getAddress().getAddress() + " port: 843");
InetSocketAddress addr = new InetSocketAddress(InetAddress.getByName(device.getAddress().getAddress()), 843);
policySocket.bind(addr);
policySocket.setReuseAddress(true);
policySocket.setReceiveBufferSize(256);
} catch (Exception e) {
e.printStackTrace();
}
anyone know how to bind to ports lower
than 1024 in Android?
Either root your phone, modify the firmware, or don't bind to ports lower than 1024. That's a Linux thing more than an Android thing.
Try add permission android.permission.INTERNET
it worked for me

Requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR

I've given READ_CALENDAR and WRITE_CALENDAR permissions in my Manifest and in my installed app I've given Calendar runtime permission and I verified the same from app's settings.
But even though, on some devices I'm getting Requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR error! This is insane! Here is my logcat:
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by java.lang.SecurityException: Permission Denial: opening provider com.android.providers.calendar.CalendarProvider2 from ProcessRecord{76fdbc0 21351:com.app.byday/u0a166} (pid=21351, uid=10166) requires android.permission.READ_CALENDAR or android.permission.WRITE_CALENDAR
at android.os.Parcel.readException(Parcel.java:1943)
at android.os.Parcel.readException(Parcel.java:1889)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4885)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:6019)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2525)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1791)
at android.content.ContentResolver.query(ContentResolver.java:749)
at android.content.ContentResolver.query(ContentResolver.java:715)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:64)
at android.content.CursorLoader.loadInBackground(CursorLoader.java:54)
at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:315)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:64)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Additional info: I've integrated Etar-AOSP calendar app in my app.
Those require runtime permissions, not just manifest permissions on any modern version of Android.
First of all, you need to add permissions to your manifest.xml For calendar, you need to add this
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
At runtime, you need to ask for permission again. So, you can create some functions to check it. Declare a vble for your callback
final int callbackId = 42;
and call to your function for checking permissions
checkPermission(callbackId, Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR);
And this is the function to declare:
private void checkPermission(int callbackId, String... permissionsId) {
boolean permissions = true;
for (String p : permissionsId) {
permissions = permissions && ContextCompat.checkSelfPermission(this, p) == PERMISSION_GRANTED;
}
if (!permissions)
ActivityCompat.requestPermissions(this, permissionsId, callbackId);
}

Security Exception for Download Manager with targetSdkVersion Below 22

I have a weird crash in Lollipopand below. I get a security Exception when trying to download a file from the server, but in devices running Marshmallow and above the app does not crash. Logcat:
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
In App's grandle I use
targetSdkVersion 22
Will change after our first push on Google Console to target the latest sdk version, but for now this will remain to 22, so no need for run time permissions.
Furthermore, in App's Manifest we declare app's permission.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Also, the code that uses the DownloadManagerinside an AsynckTask :
try {
String dirPath = String.format("%5$s/%1$s/Resources/%2$s/%3$s/%4$s/", userID, resource.getiD(), filePackage.getiD(), language, dir.getCanonicalPath());
File makeDirs = new File(dirPath);
makeDirs.mkdirs();
} catch (Exception ex) {
ex.printStackTrace();
}
this.downloadManager = (DownloadManager) getApplicationContext().getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(url);
request.setDestinationInExternalFilesDir(getApplicationContext(), null, filePath);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setVisibleInDownloadsUi(false);
downloadID = downloadManager.enqueue(request);
Again, this crash is observed only in Galaxy A5 with OS version 5.0.2, in an emulator running Samsung Galaxy S6 with Lollipop and in emulator running Kitkat 4.4.4 HTC One.
The full stack trace :
java.lang.RuntimeException: Unable to start activity ComponentInfo{...players.PDFViewer}: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.SecurityException: need WRITE_EXTERNAL_STORAGE permission to use DESTINATION_FILE_URI: uid 10229 does not have android.permission.WRITE_EXTERNAL_STORAGE.
at android.os.Parcel.readException(Parcel.java:1540)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:475)
at android.content.ContentResolver.insert(ContentResolver.java:1260)
at android.app.DownloadManager.enqueue(DownloadManager.java:1336)
at de.imsystems.crmmobile.players.PDFViewer.onCreate(PDFViewer.java:104)
at android.app.Activity.performCreate(Activity.java:6374)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2873) 
at android.app.ActivityThread.access$900(ActivityThread.java:181) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1482) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6145) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
If you have any thoughts, please share them, thanks in advance.
Eventually this post WRITE_EXTERNAL_STORAGE not working on lollipop even though it's set in the manifest helped me. What I did was to alter permissions in AndroidManifest.xml file like this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="26" tools:replace="android:maxSdkVersion"/>
This way the app doesn't crashes!

Collections.sort causing java.lang.SecurityException: Requires READ_PHONE_STATE

I am getting the crash below at this code line:
pkgMrg_global = getActivity().getPackageManager();
apps = pkgMrg_global.getInstalledApplications(0);
//GETTING CRASH AT LINE BELOW
Collections.sort(apps,
new ApplicationInfo.DisplayNameComparator(
pkgMrg_global));
What I dont understand is how Collections.sort leads to this Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10157 nor current process has android.permission.READ_PHONE_STATE?
Stacktrace:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10157 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1465)
at android.os.Parcel.readException(Parcel.java:1419)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getSubscriberId(IPhoneSubInfo.java:231)
at android.telephony.TelephonyManager.getSubscriberId(TelephonyManager.java:1352)
at android.app.ApplicationPackageManager.getSimOperatorNameForStk(ApplicationPackageManager.java:1548)
at android.app.ApplicationPackageManager.getText(ApplicationPackageManager.java:1109)
at android.content.pm.PackageItemInfo.loadLabel(PackageItemInfo.java:115)
at android.app.ApplicationPackageManager.getApplicationLabel(ApplicationPackageManager.java:1186)
at android.content.pm.ApplicationInfo$DisplayNameComparator.compare(ApplicationInfo.java:561)
at android.content.pm.ApplicationInfo$DisplayNameComparator.compare(ApplicationInfo.java:554)
at java.util.TimSort.binarySort(TimSort.java:261)
at java.util.TimSort.sort(TimSort.java:204)
at java.util.TimSort.sort(TimSort.java:169)
at java.util.Arrays.sort(Arrays.java:2010)
at java.util.Collections.sort(Collections.java:1883)
at com.mavdev.focusoutfacebook.fragments.addablock.apps.Fragment_appsselect_addblock$loadAppsListfromSystem.doInBackground(Fragment_appsselect_addblock.java:1830)
at com.mavdev.focusoutfacebook.fragments.addablock.apps.Fragment_appsselect_addblock$loadAppsListfromSystem.doInBackground(Fragment_appsselect_addblock.java:1802)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
... 4 more
As per google docs here
The note says
Note: If both your minSdkVersion and targetSdkVersion values are set to 3 or lower, the system implicitly grants your app this permission. If you don't need this permission, be sure your targetSdkVersion is 4 or higher.
Protection level: dangerous
if so you need to add the permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Constant Value: "android.permission.READ_PHONE_STATE"
add this permission to your android manifest
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

ACCESS_FINE_LOCATION permission error emulator only

I'm having a weird error while using the emulator ONLY.
I found one question that has the same issue 9 months ago with no answers at all...
I'm using google play services locations to get locations in my app and I'm sure of my manifest permissions and everything works perfectly on my phone, the problem occurs when using an emulator only, and I tried different emulators on both my iMac and Windows machines.
I doubt that emulators don't support this basic function!
here is my manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orderme.ordermeandroid" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
and here is where the exception is firing:
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient,mLocationRequest,this);
stack trace:
08-26 14:01:19.699 10157-10157/com.orderme.ordermeandroid E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.orderme.ordermeandroid, PID: 10157
java.lang.SecurityException: Client must have ACCESS_FINE_LOCATION permission to request PRIORITY_HIGH_ACCURACY locations.
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at com.google.android.gms.location.internal.zzg$zza$zza.zza(Unknown Source)
at com.google.android.gms.location.internal.zzi.zza(Unknown Source)
at com.google.android.gms.location.internal.zzj.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
at com.google.android.gms.location.internal.zzd$1.zza(Unknown Source)
at com.google.android.gms.common.api.zzc$zza.zzb(Unknown Source)
at com.google.android.gms.common.api.zzf.zza(Unknown Source)
at com.google.android.gms.common.api.zzf.zzb(Unknown Source)
at com.google.android.gms.common.api.zzi.zzb(Unknown Source)
at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
at com.orderme.ordermeandroid.Main.MainActivity.onConnected(MainActivity.java:184)
at com.google.android.gms.common.internal.zzk.zzh(Unknown Source)
at com.google.android.gms.common.api.zzg.zznt(Unknown Source)
at com.google.android.gms.common.api.zzg.onConnected(Unknown Source)
at com.google.android.gms.common.api.zzi$2.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzg.zzoD(Unknown Source)
at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
at com.google.android.gms.common.internal.zzj$zza.zzs(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzc.zzoF(Unknown Source)
at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
My guess is that you are running this on an Android 6.0+ emulator and you have a targetSdkVersion of 23 or higher.
In that case, ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION are part of the Android 6.0 runtime permission system. Either revise your app to participate in this system, or drop your targetSdkVersion below 23.
in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app
How to check if you have the permission:
// Assume thisActivity is the current activity
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
android.Manifest.permission.ACCESS_FINE_LOCATION);
How to request the permissions you need :
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
// app-defined int constant. The callback method gets the
// result of the request.
}
How to access the result of the request :
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the task you need to do.
} else {
// permission denied, boo! Disable the functionality that depends on this permission.
}
return;
}
}
}
Source : http://developer.android.com/training/permissions/requesting.html
Learn more about permissions : https://guides.codepath.com/android/Understanding-App-Permissions
You can not get the current location from emulator. But you can emulate it to test your code. Here you can find how to emulate.
EDIT1:
There is a note here
Note: If you are using both NETWORK_PROVIDER and GPS_PROVIDER, then you need to request only the ACCESS_FINE_LOCATION permission, because it includes permission for both providers. (Permission for ACCESS_COARSE_LOCATION includes permission only for NETWORK_PROVIDER.)
You should use only ACCESS_FINE_LOCATION permission. Maybe this helps.
The issue was caused by a new feature for Android 6.0. hope its work...
Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.
http://developer.android.com/training/permissions/requesting.html
To get around the issue when the permission is not explicitly granted, I had put in a check to wrap location service calls (as per the Google Documentation, slightly modified).
int permissionCheck = ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
//Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
}
The Google Documentation also steps through how to request for the permissions that are needed.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
some times manifest have a missing line granting the permission of ACCESS_FINE_LOCATION. so you need to mention it in the manifest.
I did this and my problem was solved.
This generally happens because we have given the permission in the manifest so the app would create a problem as it would not be allowed to access the permissions and the file would crash.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

Categories

Resources