I have updated my Android Studio to 3.4.1 and Im using target sdk version 28 . I run my previous project. Now I got many errors : getContex or ContextCompat.checkSelfPermission() is not defined.
UPDATE: here is the error:
error: cannot find symbol method getContext()
error: cannot find symbol method shouldShowRequestPermissionRationale(MainActivity,String)
How can I fix it?
ContextCompat.checkSelfPermission(Context context, String permission) method is used simply for checking whether you are having requested permission available for your app or not.
If your app needs a dangerous permission, you must check whether you have that permission every time you perform an operation that requires that permission. Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level.
ActivityCompat.requestPermissions(Activity activity, String[] permissions,int reqCode) method is used for requesting permissions
Thanks for all answers.I add implementation 'com.android.support:support-v4:28.0.0' to build.gradle. Before it I just have implementation 'com.android.support:appcompat-v7:28.0.0' and It works.
Related
I am building an SDK API with beacons integration which requires Bluetooth scan permission for android 12 and above. So, I added annotation for the API like this:
#RequiresPermission(allOf = [Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.BLUETOOTH_SCAN])
fun enableBeaconScan()
When I add this, I am getting a warning:
Is there a way to restrict this permission only for the supported android versions and get rid of this warning?
you can use this annotation
#TargetApi(Build.VERSION_CODES.S)
i'm trying to get Serial Number in android
in this answer Thaussma said that
in Android 8 and later (>= SDK 26) use android.os.Build.getSerial()
which requires the dangerous permission READ_PHONE_STATE. Using
android.os.Build.SERIAL returns android.os.Build.UNKNOWN.
but when i tried android.os.Build.getSerial() with required permission i got cannot resolve method 'getSerial()' error
I have the following code-lines:
FingerprintManager fm = activity.getSystemService(FingerprintManager.class);
fm.isHardwareDetected();
At this Point, Android Studio complains about a missing permssionCheck (checkSelfPermission).
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException
However, as I understand the Documentation, USE_FINGERPRINT (required by FingerprintManager) is not a "dangerous" permission and thus is granted at install time. If I suppress the AndroidStudio warning everything works fine on my test devices.
Is this a bogus warning?
Did you add the permission to your Manifest file already? You don't need to request the permission at runtime, but you still need to have the manifest entry.
This clearly seems to be an AndroidStudio bug. I have added
//noinspection MissingPermission
to supress this warning, and there are no negative consequences.
I made a very, very small demo app reproducing the bug on a Nexus 5 running Android version 6.0.1. The app is on github here: https://github.com/lexi-sr/LayerPermission
I recently added 2 commits in which it targets API 23 and requests permissions at run time, but it still didn't work.
In these 2 commits, it has these settings:
Target SDK: 23
compiles Layer 0.20.3
1) In the commit "Removing layer dependency allows the popup dialog to request the perm…", where it does NOT have layer dependency:
The method ActivityCompat.requestPermissions opens a dialog that requests the Contacts permission, and a log statement within the onRequestPermissionsResult method logs that the permission has been granted.
2) In the commit "Requesting permissions does not work" where it DOES have layer dependency:
ActivityCompat.requestPermissions does not open up any dialogs, but a log statement within onRequestPermissionsResult still prints, logging that it does not have the permission.
It seems like adding the layer dependency is suppressing the ability to request for permissions at run time. Why is this happening?
Luckily, the layer support team was able to help me with this. It solved my problem in the demo app (which targeted SDK 23) and my real app (which targeted SDK 22, to avoid requesting permissions at runtime). After I put tools:node="replace" into my uses-permission line for GET_ACCOUNTS, the pop up dialog was able to appear and grant the permission in the demo app, and the permission was no longer missing in the real app which targeted SDK 22.
Here is the detailed explanation from the Layer support team:
The layer SDK requests the GET_ACCOUNTS permission using a
maxSdkVersion of 18. It would appear that when the manifests get
merged this is overwriting the permission request in your manifest,
thus not requesting that permission for 19+. Could you try appending
tools:node="replace" to the permission in your app's manifest? The
line should read as:
<uses-permission android:name="android.permission.GET_ACCOUNTS" tools:node="replace" />
See here for the maxSdkVersion documentation:
http://developer.android.com/guide/topics/manifest/uses-permission-element.html
See here for the tools:node documentation:
http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger#TOC-tools:node-markers
Peter
I'm looking for a quick way to identify missing Android permissions. According to Android Tools Project Site here: http://tools.android.com/tips/lint-checks, Android Studio 1.4 is able to check MissingPermission:
MissingPermission
Priority: 9 / 10 Severity: Error Category: Correctness
This check scans through your code and libraries and looks at the APIs
being used, and checks this against the set of permissions required to
access those APIs. If the code using those APIs is called at runtime,
then the program will crash.
Furthermore, for permissions that are revocable (with targetSdkVersion
23), client code must also be prepared to handle the calls throwing an
exception if the user rejects the request for permission at runtime.
However, when I try ./gradlew lint check MissingPermission, the output is:
Task 'MissingPermission' not found in root project 'Android'.
(I have to use the command line because AS always freezes when I use Analyze - Inspect Code from UI.)
If I run ./gradlew lint, Lint report doesn't complain missing permission even if I remove android.permission.INTERNET.
Any ideas on this Lint option?