How to Allow notification sound programmatically in Xiomi devices - android

Notification sound settings always disable in Xiomi devices. Check below image.
I want to enable sound programatically. Found similar stackoverflow questions but nothing helped.
Device : Redmi Note 5 pro, Redmi Note 9 pro
OS : MIUI 11 , MIUI 12
Note: But it works fine in all other devices. Problem only in Xiomi devices

I was looking for the same kind of issue 2 months ago, due to restrictions by Xiaomi it has become a lot harder, basically you have to have your app running without restriction in save battery settings and to bring the user to autostart where he will have to click on the toggle button to add your app.
Prompt the user to disable battery optimization for your app:
public class MainActivity extends AppCompatActivity {
#Override
protected void onStart()
{
String packageName = "com.example.myapp";
PowerManager powerManager = (PowerManager)getApplicationContext().getSystemService(POWER_SERVICE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent i = new Intent();
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
}
}
}
For an app to use this, it also must hold the Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission
You also have to prompt the user to add your app to autostart (original credit) :
String manufacturer = "xiaomi";
if (manufacturer.equalsIgnoreCase(android.os.Build.MANUFACTURER)) {
//this will open auto start screen where user can enable permission for your app
Intent intent1 = new Intent();
intent1.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent1);
}
It should now be working, let me know if it helped!

You may try to use NotificationManager.IMPORTANCE_MAX in new NotificationChannel

Related

How to check, detect or identify the settings component screen overlay or any feature available or not

My android app need display over other app permission which is screen overlay permission.
Settings.ACTION_MANAGE_OVERLAY_PERMISSION
SYSTEM_ALERT_WINDOW
In normal android OS device i can give permission easily using this code.
if (!Settings.canDrawOverlays(this) && !Constant.IsOverlayPermissionGiven) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivityForResult(intent, REQUEST_CODE);
handler.postDelayed(checkSettingOn, 200);
return false;
} else {
return true;
}
But in Android Go edition device permission screen displaying the message of "This feature is not available" with the caution of "This feature has been turned off because it slows down your phone"
How to get the information about the feature is available or not programmatically ?
How to bypass the permission screen which device don't have screen overlay feature?
If you read the Android 11 source code, you can see that it only uses ActivityManager.isLowRamDevice() to check whether the SYSTEM_ALERT_WINDOW feature is available or not
packages\apps\Settings\src\com\android\settings\Utils.java
/**
* Returns true if SYSTEM_ALERT_WINDOW permission is available.
* Starting from Q, SYSTEM_ALERT_WINDOW is disabled on low ram phones.
*/
public static boolean isSystemAlertWindowEnabled(Context context) {
// SYSTEM_ALERT_WINDOW is disabled on on low ram devices starting from Q
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
return !(am.isLowRamDevice() && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q));
}

Android O+: Some phones seem to lack the option to change sound type for notifications

Again about notification sound on Android O+.
There are some phones where the "notification settings" window doesn't show the sound selection button (and not even the vibration button).
Here are a couple examples:
Samsung A5
Huawei Honor View 10
(not minor brands... I would say)
They were tested with Gmail app (menu -> Settings -> account -> Notification settings) on Android 8.
Here Android O - Notification Channels - Change Vibration Pattern or Sound Type is a solution to avoid the "standard" window, but why should we reinvent the wheel?
Is there any other option that I'm missing?
Thank you,
Max
P.S.
Here is a screenshot from a Honor 9 / Android 8.0.0.
Channel name is "Mail" ("Posta" in Italian). For sound ("Suoneria" in Italian) there is only an On/Off switch.
It's a mess. You need to add workarounds for the different brands/devices. This is the flow we're using to deal with it:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !isDeviceWithWorkaround()) {
// Send to notification channel settings (See https://developer.android.com/training/notify-user/channels#UpdateChannel)
}else{
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Sound");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(someExistingRingTone));
if (isDeviceWithWorkaround()) {
intent.setPackage("com.android.providers.media");
}
try {
startActivityForResult(intent, reqCode);
} catch (ActivityNotFoundException e) {
if (isDeviceWithWorkaround()) {
Log.i(TAG, "Failed to find preferred package [" + intent.getPackage() + "]. Trying again without package.");
intent.setPackage(null);
startActivity(intent);
}
}
}
So what's happening is that if it's a device with a known issue as you talk about we send them to the good old ringtone picker.
I believe that the package com.android.providers.media doesn't have an activity to start on stock Android, but on Huawei it then opens the Media Store where we get back a ringtone URI that can be used as notification sound. (We don't want the user to end up in some other ringtone picker that might not work. We have always recommended our users to use https://play.google.com/store/apps/details?id=com.angryredplanet.android.rings_extended but it won't work with Huawei on Android 8).

PowerManager.isIgnoringBatteryOptimizations always returns true even if is removed from 'Not optimized apps'

I have a mileage logbook app which does GPS tracking and is able to establish a OBDII connection to a car in background.
Now I want to show a Popup which informs the users if my app is not whitelisted in doze since this may stop my background (actually foreground) services...
I do:
String PACKAGE_NAME = getApplicationContext().getPackageName();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
boolean status = false;
status = pm.isIgnoringBatteryOptimizations(PACKAGE_NAME);
if (!status) {
// show popup
}
but PowerManager.isIgnoringBatteryOptimizations always returns 'true' even if it is removed from 'Not optimized apps' again. Only if I uninstall the app 'false' is returned again...
Tested on Galaxy Note 8 (Android 8.0) and Emulator 8.1
Question is simple: Is this a bug? Or how to remove the app from whitelist so that PowerManager.isIgnoringBatteryOptimizations is returnung 'false' again?
I have made simple one activity app that requests
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
and put this code into onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
String packageName = this.getPackageName();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
this.startActivity(intent);
}
I have tested it on 8.1 emulator and I got the same behavior. You can possibly unisntall app or switch its state from optimized to not optimized in all app list in settings. I guess Bob Snyder showed right issue in google tracker.
Same here. Android 9 emulator.
Uninstalling the app and installing it again makes the method to work again.

How to request Do Not Disturb permission on Samsung S5 (SM-G800H)?

This is the usual way to request DND permission:
Intent intent = new Intent();
intent.setAction("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS");
startActivityForResult(intent, NOTIFICATION_REQUEST_CODE);
However on (at least one) Samsung SM-G800H this throws ActivityNotFoundException.
Apparently on that model they have something called 'Blocking Mode'. Anyone know the correct intent / how to handle on that model?
See https://www.cnet.com/uk/how-to/where-to-find-do-not-disturb-mode-on-the-galaxy-s5/
Device details:
Manufacturer: Samsung
Model: SM-G800H
Board: Msm8228
Android API: 23
Android OS: 6.0.1
Brand: Samsung
RAM: 1.35GB
Orientation: Portrait
This might not be a Samsung related issue. According to the official docs, ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS is available only on API 23+ (Marshmallow). Does the S5 you're testing on have Android 6 Marshmallow running? If not, that's your problem.
EDIT: Since this Activity may not be present on each device even if they are on Android 6, you'll need to handle the case where the intent can't be resolved. So something like this might be what you want:
PackageManager packageManager = getActivity().getPackageManager();
Intent intent = new Intent();
intent.setAction("android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS");
if (intent.resolveActivity(packageManager) != null) {
startActivityForResult(intent, NOTIFICATION_REQUEST_CODE);
} else {
Log.d(TAG, "No Intent available to handle action");
}

How can I change the Audio Mode (Silent, Normal) in Android 7.0 without Opening Setting Intent?

I want to change my android device audio mode from Normal to silent and silent to normal. `
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(!silent) {
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
silent = true;
}
else {
audio.setRingerMode( AudioManager.RINGER_MODE_VIBRATE );
silent = false;
}
`
This is my code that i'm using. Perfectly running until Android 6 all versions.
But on Android 7+ it crashes.
I found a solution on web that opens a settings intent and get some permission from user then this code works fine. It is the code i used then
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
But my project requirement is don't request to open a setting intent to allow it form there. Is should be work like getting user permission to change mode on same activity just like all other permissions i.e. location, read contacts etc.
I tried to find out the find how they implement the permission in the code file but did not find it. Please let me know if anyone know.
This is the only way to change the Audio Mode of Android with api level > 23
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
Now officially its released by Android to get user permission to access Notification Policy of device to change the audio Mode.

Categories

Resources