Since I can use Settings.canDrawOverlays to check if the user granted this permission on API >= 23, how I can check if the user have it on older APIS?
Does this permission is automically granted on API < 23 and no need to check for it?
Currently, I start my service only on API 23+ after the permission are granted.
#SuppressLint("NewApi")
public void checkDrawOverlayPermission() {
if(Build.VERSION.SDK_INT >= 23) {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
} else {
startService(new Intent(this, ChatHeadService.class));
}
}
}
But what if the user is on API <= 22? How I can make sure the application wont crash and my service will start?
Overlay permission is only required for Marshmallow (API 23) and above. In previous APIs this permission is provided by default.
Related
I am using Android 13 device for testing. Notification is receiving on version 11 and below but not on 12 and above. I have implemented notification permission also explored some stack answers but still did not find and solution. From firebase if i directly send test notification then it receives but it does not receiving from backend when some action is performed in app.
In Android 12 and above, you also need to request the "group notification" permission.
here is an example how it can looks like:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.GROUP_NOTIFICATION)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.GROUP_NOTIFICATION},
REQUEST_GROUP_NOTIFICATION_PERMISSION);
}
}
And your have to add POST_NOTIFICATIONS permission
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS},101);
}
else {
createChannel();
}
}
I have an application that is meant for "locked Android phones" meaning the application is the only application users will have access to on that phone and that is done using MySync.
Before the application had to support Android 11 (complieSdkVersion 30), the user was able to change screen brightness and other system settings from the application (Since the user did not have access to the settings app). But now the WRITE_SETTINGS and CHANGE_CONFIGURATION are deprecated and no longer have desirable effects.
I am aware that it is possible to prompt the user to accept the settings with this prompt:
boolean permission;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
permission = Settings.System.canWrite(context);
} else {
permission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED;
}
if (permission) {
initApp();
} else {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, SettingsActivity.CODE_WRITE_SETTINGS_PERMISSION);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_SETTINGS}, SettingsActivity.CODE_WRITE_SETTINGS_PERMISSION);
}
}
But since the user does not have access to the settings application in my app eco-system, I can not give the user a prompt that would open up the settings and allow the user to stay in the settings.
Is there no way around this permission request and is there no way to do it on the manifest level or some other way?
Apparently the MIUI OS has already implemented its own Permissions system before Marshmallow does. I'm currently testing a video recording app for the Xiaomi Mi 4i, which uses a MIUI based on API 21 [Android 5.0.2], and it needs the Record Audio permission which is not granted by default by MIUI's Permissions Manager.
So far the way I've managed to alter the permissions is by accessing the Permissions Manager window for the app on clicking the OK button in the AlertDialog:
isMIUI = MIUIUtils.isMIUI();
if(isMIUI)
{
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setMessage("If you intend to use the video recording feature, please enable the 'Record Audio' permission in the settings menu. You will be redirected there if you press OK.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
intent.putExtra("extra_pkgname", "com.picmix.mobile");
startActivity(intent);
}
})
.setNegativeButton("CANCEL", null)
.create();
adb.show();
}
But this isn't good enough for me. I need to check if the Record Audio permission is already checked in the MIUI Permissions Manager in order to run this only once.
How do I check for the permissions granted or to be notified in the MIUI Permissions Manager programmatically?
private boolean resourceCanBeAccessed() {
boolean response = true;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
== PackageManager.PERMISSION_DENIED ) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.RECORD_AUDIO}, 1);
response = false;
}
}
return response;
}
You just need to call this method before accessing the resource. This method will return true if the permission is granted. It the permission is not granted then it will grant the permission
I've noticed someone who is using my app reported a crash which logged by the Google Developer Console:
java.lang.NoSuchMethodError: No static method canDrawOverlays(Landroid/content/Context;)Z in class Landroid/provider/Settings; or its super classes (declaration of 'android.provider.Settings' appears in /system/framework/framework.jar)
at com.pack.MainActivity.checkDrawOverlayPermission(MainActivity.java:311)
at com.pack.MainActivity.onCreate(MainActivity.java:127)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2397)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
The canDrawOverlays is an API 23+ method and I Use it like that:
/** code to post/handler request for permission */
public final static int REQUEST_CODE = 100; /*(see edit II)*/
#SuppressLint("NewApi")
public void checkDrawOverlayPermission() {
/** check if we already have permission to draw over other apps */
if (!Settings.canDrawOverlays(this)) {
/** if not construct intent to request permission */
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
}
And I have this in my MainActivity:
checkDrawOverlayPermission();
The device which crashed using Android 5.1
HOw I can make sure my app will work on ANdroid 5.1? (API 22 and below) who don't have this method which available from API 23 and up?
Check the current API of the device which runs your code. If it >= 23, you can use the code
if(Build.VERSION.SDK_INT >= 23) {
// if (!Settings.canDrawOverlays(this)) {
//
} else {
// another similar method that supports device have API < 23
}
Use this
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
/** request permission via start activity for result */
startActivityForResult(intent, REQUEST_CODE);
}
} else {
}
For all methods that appeared in newer SDK versions you should use the following pattern:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// code for Marshmallow (SDK 23) and newer versions
} else {
// fallback for older versions
}
I'm trying to programmatically call to a number with following code:
String number = ("tel:" + numTxt.getText());
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(number));
startActivity(intent);
I've set the permission in the Manifest:
<uses-permission android:name="android.permission.CALL_PHONE"/>
I'm working with real device for testing and debugging, it is Nexus 5 with Android M, my compileSdkVersion is 23. I'm getting the following Security Exception:
error: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxxxxxxxxx cmp=com.android.server.telecom/.components.UserCallActivity } from ProcessRecord{cbbd7c1 5228:com.dialerTest.DialerApp/u0a96} (pid=5228, uid=10096) with revoked permission android.permission.CALL_PHONE
I've searched the web and this community for similar Q/A and couldn't find the answer. Any help will be appreciated.
Permission CALL_PHONE belong to dangerous permission group.
So if your apps target SDK is 23 or higher and your device is running on Android 6.0 or higher, you must request for CALL_PHONE permission while the app is running.
Example :
String number = ("tel:" + numTxt.getText());
mIntent = new Intent(Intent.ACTION_CALL);
mIntent.setData(Uri.parse(number));
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
// MY_PERMISSIONS_REQUEST_CALL_PHONE is an
// app-defined int constant. The callback method gets the
// result of the request.
} else {
//You already have permission
try {
startActivity(mIntent);
} catch(SecurityException e) {
e.printStackTrace();
}
}
When your app requests permissions, the system presents a dialog box to the user. When the user responds, the system invokes your app's onRequestPermissionsResult() method, passing it the user response.
#Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the phone call
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
In android 6.0 (Api lvl 23) we have something called "Runtime Permissions". You have to read about it.
You can find documentation here.
You code could works only if you make an ACTION_DIAL, not an ACTION_CALL where you need to request a permission, so if you want to make a call, please follow this example:
MANIFEST:
<uses-permission android:name="android.permission.CALL_PHONE" />
Code:
import static android.Manifest.permission.CALL_PHONE;
Intent i = new Intent(Intent.ACTION_CALL);
i.setData(Uri.parse("tel:0612312312"));
/*
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:0612312312"));
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}*/
if (ContextCompat.checkSelfPermission(getApplicationContext(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(i);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
in fragment class
Step 1:
import static android.Manifest.permission.CALL_PHONE;
step 2:Where your onclick button:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +driver_no ));
if (ContextCompat.checkSelfPermission(getActivity(), CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(callIntent);
} else {
requestPermissions(new String[]{CALL_PHONE}, 1);
}
[Or] you are using in activity class means change the getActivity to getApplicationContext()
FYI: If you're targeting Android 11, you need to add an Intent Query element for Intent.ACTION_DIAL in your manifest.
https://developer.android.com/training/basics/intents/package-visibility#intent-signature