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

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");
}

Related

How to Allow notification sound programmatically in Xiomi devices

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

How to run Android app or install on PlayStore if unavailable for API30?

I have a android app where I want to run other apps. If these apps are not installed already, I would like to open PlayStore to let the user install them:
var info : ApplicationInfo? = null
try {
info = pm.getApplicationInfo(packageName, 0) as ApplicationInfo
} catch (e: PackageManager.NameNotFoundException){
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("market://details?id=$packageName")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
if (info != null) {
val intent = pm.getintentForPackage(packageName) as Intent
context.startActivity(intent)
}
This snippet works so far good for API 26 (Android 8) for instance. But for API 30 (Android 11) the getApplicationInfo() throws a NameNotFoundException always, even when I already installed the app.
Whats wrong here?
On Android 11, methods on PackageManager like getApplicationInfo() no longer work by default. You need to either:
Not call them, or
Add a <queries> element to your manifest, whitelisting the third-party apps that you wish to find, or
Request the MANAGE_ALL_APPS permission (and run the risk of your app being banned from the Play Store)
In your case, the <queries> element is probably a safe choice, assuming that your list of application IDs that you are looking for is knowable at compile time.

Android TV Set screensaver intent

There's no possibility to open preview of screensaver like for a live wallpaper so I'd like to provide a shortcut for users to open TV's settings and manually set screensaver.
Currently I open system settings with intent action android.settings.SETTINGS which opens root device settings.
On phones I can start android.settings.DREAM_SETTINGS (it is described in docs here) which opens corresponding settings section. But this doesn't work on TV (tried on emulator). Is it possible to open it on Android TV?
Just in case somebody looks for launching screensaver on Nvidia Shield.
The intent name is com.android.systemui/.Somnambulator.
If using Button Mapper app, use "Broadcase Intent" with "Package": com.android.systemui and "Component": com.android.systemui.Somnambulator.
I've found a way to launch this activity in the source code of "Aerial Dream" screensaver: https://github.com/cachapa/AerialDream/blob/master/app/src/main/java/com/codingbuffalo/aerialdream/SettingsFragment.java#L29
// Check if the daydream intent is available - some devices (e.g. NVidia Shield) do not support it
Intent intent = new Intent(SCREENSAVER_SETTINGS);
if (!intentAvailable(intent)) {
// Try opening the daydream settings activity directly: https://gist.github.com/reines/bc798a2cb539f51877bb279125092104
intent = new Intent(Intent.ACTION_MAIN).setClassName("com.android.tv.settings", "com.android.tv.settings.device.display.daydream.DaydreamActivity");
if (!intentAvailable(intent)) {
// If all else fails, open the normal settings screen
intent = new Intent(SETTINGS);
}
}
startActivity(intent);
private boolean intentAvailable(Intent intent) {
PackageManager manager = getActivity().getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
return !infos.isEmpty();
}

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.

Is there any android virtual device that has no phone service ?

I want to have a virtual android pad that has no phone service so that the following code of mine will work:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + getPhoneNumber()));
if (intent.resolveActivity(getPackageManager()) == null) {
//do alert
}
However, I don't know which of the out-of-box virtual device has no calling service. I tried "10.1 WXGA (Tablet)", but it has calling service embedded. What should I do?
What about creating a unit test and testing your code without be depending on that device?
Google developers is a good place to start.

Categories

Resources