Android M Error on api 22, and requestPermissions() method not found - android

I'm working in API 22, but I want to compile my project in Android M 6.0, I have this code:
Declared at the top:
private static final String[] REQUIRED_PERMISSIONS = new String[]{"READ_EXTERNAL_STORAGE"};
private static final int REQUEST_PERMISSIONS = (Integer) null;
And on my onCreate():
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
LinkedList<String> missingPermissions = new LinkedList<>();
for(String p : REQUIRED_PERMISSIONS){
if(checkCallingOrSelfPermission(p) != PackageManager.PERMISSION_GRANTED){
missingPermissions.add(p);
}
}
if(!missingPermissions.isEmpty()){
String[] mpArray = new String[missingPermissions.size()];
missingPermissions.toArray(mpArray);
requestPermissions(mpArray, REQUEST_PERMISSIONS);
}
}
I was inspired here for checking my problem
and in Eclipse is giving me an error on Build.VERSION_CODES.M(M not found), and then, the callback method requestPermissions(mpArray, REQUEST_PERMISSIONS) isn't found too, any suggestion?
If I'm working on API 22, and I'm compiling with Android 6.0 M. How I can solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?

As per the documentation, Build.VERSION_CODES.M and requestPermissions() were added in API level 23.
Since you are compiling with API level 22, those simply do not exist.
To access APIs introduced in API level 23, you need to compile with API 23. You cannot access these APIs if you continue to compile with API 22.
Note that simply compiling with API 23 will not affect the way your application behaves on any devices, it simply opens up the newer APIs for your use on devices running at least API 23.
if I'm working on API 22, and I'm compiling with Android 6.0 M, how i can do for solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?
Devices running API 22 will continue to use the old install-time model for permissions. Nothing has changed for devices running API 22 and below. Only devices running API 23 use the new runtime permissions model.

Related

FIleObserver NoSuchMethodError in Android 10

I have a FileObserver(File dir) that works perfectly in Android11, but crashes badly in Android10 with the following LogCat even when I try to access the own APP private folder (external sdcard):
E/AndroidRuntime(24651): java.lang.NoSuchMethodError: No direct method <init>(Ljava/io/File;)V
in class Landroid/os/FileObserver; or its super classes (declaration of
'android.os.FileObserver' appears in /system/framework/framework.jar!classes2.dex)
E/AndroidRuntime(24651): at com.floritfoto.apps.xvf.FileChooser$1.<init>(FileChooser.java:570)
E/AndroidRuntime(24651): at com.floritfoto.apps.xvf.FileChooser.watchdir(FileChooser.java:570)
E/AndroidRuntime(24651): at com.floritfoto.apps.xvf.FileChooser.access$500(FileChooser.java:53)
My code is pretty simple:
private void watchdir(final File dir) {
if (observer != null) {
observer.stopWatching();
observer = null;
}
observer = new FileObserver(dir) { // <===== CRASHES HERE
#Override
public void onEvent(int event, String path) {
event &= FileObserver.ALL_EVENTS;
....
}
};
observer.startWatching();
}
Changing observer = new FileObserver(dir) with observer = new FileObserver(dir.getAbsolutePath()) works fine, but FileObserver(String path) is deprecated.
My app has compileSdkVersion 31, targetSdkVersion 31, and minSdkVersion 14.
At least to some extent this error has been reported before, but no solution has been provided yet.
EDIT: The crash is in Android 9, not 10.
FileObserver(File) is new to API Level 29. Your minSdkVersion is 14. Your code will crash with this error on API Level 28 and lower. You should be getting Lint warnings and the like warning you that FileObserver(File) is newer than your minSdkVersion.
Your options are:
Use FileObserver(String) on all API levels, despite the deprecation warning.
Use FileObserver(String) on API Levels 14-28 and use FileObserver(File) on API Level 29+, using Build.VERSION.SDK_INT to determine the API level of the device your app is running on.
Set your minSdkVersion to 29.
Only use FileObserver on API Level 29+, and skip whatever features depend on it on older devices.

requestPermissions() method is showing error warning in Activity

I used requestPermissions() method in fragment and its working fine without showing any error or warning but when i use the same code in Activity for same purpose its working fine but showing an error warning. The following code i used in both fragment and Activity
requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE, CAMERA}, PERMISSION_REQUEST_CODE);
Its showing like this
Code is running correctly but its showing like this only in Activity not in fragment, Why and how can i fix this ?.
In your AndroidManifest.xml you have specified android:minSdkVersion = "21" but this API is only available on SDK versions 23 and up.
You have to check the android version first or your app will crash on android versions 5.0 (SDK 21) and 5.1 (SDK 22)
For cheking and supporting different android versions refer to https://developer.android.com/training/basics/supporting-devices/platforms
Your minimum api level version is 21 but requestPermission method works from version level 23.
So, to use requestPermission() method you must handle the version code-
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermission(............................)
}
Problem : Runtime Permission is part of MarshMallow & Above. Your current min API is 21(Lollipop) which does not have runtime permission.
Solution: Either you need to check whether current API is above Marshmallow or use ActivityCompat
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);
}
Or
ActivityCompat.requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, PERMISSION_REQUEST_CODE);

Permissions are granted by default

I'm not sure i fully understand this. So, for the <= 21 API version we can just use AndroidManifest.xml to request permissions, but Lollipop and higher APIs we have Requesting permission on runtime feature. So i'm using it with this simpe code:
if (Build.VERSION.SDK_INT >= 23) {
mPermissionsToBeAsked.clear();
for (String permission : AudioRecordingThread.PERMISSIONS_NEEDED) {
if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
mPermissionsToBeAsked.add(permission);
}
} ....
Then, if that list is not empty i'm requesting them :
if (mPermissionsToBeAsked.size() > 0) {
requestPermissions(mPermissionsToBeAsked.toArray(new String[0]), AUDIO_PERMISSIONS_REQUEST_CODE);
}
But, for some reason, on devices, for example, like Samsung Galaxy S7 with Android 6.0.1, all the permissions grandted by default when app is installed. So i want to know why, BUT, it's there is an even bigger concerne, when i go to my application in Application Manager and manually removing Microphone permision, in the app checkSelfPermission(permission) is still returning GRANTED. So the questions:
Why on devices with API level Lollipop and higher all permissions are still granted by default and above code won't add anything into mPersmissionToBeAsked?
Why if i manually removing permission with title MICROPHONE in Application manager checkSelfPermission(android.permission.RECORD_AUDIO) still returns GRANTED?
Just Cross verify in your app gradle file the targetsdk version is greater than 22.
defaultConfig {
// -----
targetSdkVersion 23
//----
}
If it is less than 23 than permission will automatically been granted to your app.
First of all it's Android M and above that handles permission granting. And that means you should have
targetSdkVersion 23
or above. Otherwise the system considers that the developper did not target this version, meaning that the developper does not check for permissions.

Use PopupWindow.setWindowLayoutType() on API < 23

It says method call requires API level 23. My code below,
But it is not possible because, following is from android official android.widget.Editor.java source file in API 19.
Also associated constants TYPE_APPLICATION_SUB_PANEL was added from api level 1. Also I used this successfully on Xamarin under api 14.
What is wrong here?
It simply show that setWindowLayoutType method does not available for API level 14.
you have to add API level condition.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
}
Did you install api 23 sdk? You must download from android sdk manager.

Call requires API level 16 (current min is 14)

I have this line in my code:
linearLayout.setBackground(drawable);
setBackground() shows the following error:
Call requires API level 16 (current min is 14)
What does this mean?
Can I raise my API level?
What does a higher/lower API level mean?
Does it limit the amount of devices my app can be used on if the API level is higher?
What does this mean?
It means that the setBackground(Drawable) method was added in API Level 16, and older devices do not have it. However, your app's minSdkVersion is 14. So, unless you take steps to avoid this line on API Level 14 and 15 devices, your app will crash on those devices.
Can I raise my API level?
You could set your minSdkVersion to 16.
Does it limit the amount of devices my app can be used on if the API level is higher?
Yes. If you say that your minSdkVersion is 16, then devices running a version of Android older than that cannot run your app.
At the time of this writing, about 90% of Android devices accessing the Play Store are on API Level 16 or higher (i.e., are running Android 4.1 or higher).
You can read more about the concept of API levels in the documentation. Note that this documentation is a bit old, in that it focuses on the minSdkVersion being defined in the manifest. In Android Studio projects, minSdkVersion is usually defined in your app module's build.gradle file.
Can I raise my API level?
You can set API to 16 it will limit device below 16 but it will perfectly work on devices API 16 and higher
Or else You can use API check and set
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable( getResources().getDrawable(R.drawable.ready) );
} else {
layout.setBackground( getResources().getDrawable(R.drawable.ready));
}
Use a validation method that will be supported only in API LEVEL >= 16 (note the use of ContextCompat class:
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
linearLayout.setBackground(ContextCompat.getDrawable(this, drawableid));
}
Use:
layout.setBackgroudResource(R.drawable.ready);

Categories

Resources