Opening Do Not Disturb preferences in Oreo - android

I'm having troubles opening system DND preferences from my App so that user can create or edit Automatic Time rule.
Current situation
Our app already has a similar feature which disables App-notification LED, sound and vibration for some specific time period (for example between 10pm-8am) and is applied app-wide. As of Android Oreo, our feature doesn't work anymore because of Notification Channels. The only solution is, as far as I understand, to create in System preferences Automatic Time rule which is then applied system-wide.
What I want to do?
Just to redirect Oreo user from my app to System preferences ie. Do Not Disturb preferences in order to add or edit Time rule.
The problem
There is no specific Intent which opens Do Not Disturb preferences. The closest one I could find was Settings.ACTION_ZEN_MODE_PRIORITY_SETTINGS which leads me to this Preference screen. I also found action which I exactly need, but as you can see, it is hidden by annotation.
Does this mean there is no way to open this Preference screen and I should use another approach?

I also had this question for a long time, now I finally found the solution that works for me:
Java
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$ZenModeSettingsActivity"));
startActivity(intent);
Kotlin
val intent = Intent()
intent.component = ComponentName("com.android.settings", "com.android.settings.Settings\$ZenModeSettingsActivity")
startActivity(intent)

If you take a look at the AndroidManifest.xml for the Settings app you can see that there is an Activity Settings$ZenModeSettingsActivity (as mentioned by #cyb3rko in https://stackoverflow.com/a/63713587/467650) already from Android 5.0.
To send the user to the "Do not disturb" screen you can use the action android.settings.ZEN_MODE_SETTINGS like this:
try {
startActivity(new Intent("android.settings.ZEN_MODE_SETTINGS"));
} catch (ActivityNotFoundException e) {
// TODO: Handle activity not found
}
I would expect the intent filter to be even more stable than the class name.

Related

What's the Intent to open DND settings?

I'm sure I'm overlooking something in the Settings class documentation. What Intent can open the Settings app in the "Do not disturb" section?
I expected it to be the ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS action, but that is only for the screen which lists which apps have requested DND access.
Update
Looking at the AndroidManifest.xml for the Settings app there is an Activity Settings$ZenModeSettingsActivity already from Android 5.0.
To send the user to the "Do not disturb" screen you can use the action android.settings.ZEN_MODE_SETTINGS like this:
try {
startActivity(new Intent("android.settings.ZEN_MODE_SETTINGS"));
} catch (ActivityNotFoundException e) {
// TODO: Handle activity not found
}
Original answer
It looks like there are no screens in the Settings app (at least on Android 6+7) where you can enable/disable DND. It seems like this is only available through the settings tile (and can be disabled in the dialog when changing the volume).
I have a Samsung S6 (Android 6.0.1) which has this screen, but this is probably some custom Samsung changes. The screen is represented by the class com.android.settings.Settings$ZenModeDNDSettingsActivity which can be started by any app. This might be of help for some people out there.
AndroidManifest.xml for Settings app for Android 6+7:
https://android.googlesource.com/platform/packages/apps/Settings/+/android-6.0.1_r68/AndroidManifest.xml
https://android.googlesource.com/platform/packages/apps/Settings/+/android-7.0.0_r6/AndroidManifest.xml
You have to use the following Intent: ACTION_VOICE_CONTROL_DO_NOT_DISTURB_MODE and then pass a boolean through EXTRA_DO_NOT_DISTURB_MODE_ENABLED.
Make note that the documentation specifies the following: This intent MUST be started using startVoiceActivity.

How to display only specific Settings fragment from android inbuilt Settings app

I want to show only wi-fi settings fragment from settings. I have achieved this by using following code:
Intent i = new Intent(Settings.ACTION_SETTINGS);
i.putExtra(":android:show_fragment", "com.android.settings.wifi.WifiSettings");
i.putExtra(":android:no_headers", true);
startActivity(i);
Now I want to show only date & time fragment from settings so I changed my code to:
Intent i = new Intent(Settings.ACTION_SETTINGS);
i.putExtra(":android:show_fragment","com.android.settings.ACTION_DATE_SETTINGS");
i.putExtra(":android:no_headers", true);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
unfortunately it's not working.How can I display only specific Settings fragment from android inbuilt Settings app like this
I want to show only wi-fi settings fragment from settings. I have achieved this by using following code:
I would recommend that you use ACTION_WIFI_SETTINGS and get rid of the undocumented extras.
Now I want to show only date & time fragment from settings so I changed my code... unfortunately it's not working.
The right solution is to use ACTION_DATE_SETTINGS and get rid of the undocumented extras. Beyond that, com.android.settings.ACTION_DATE_SETTINGS is not the name of a fragment.
Evidently no_headers was silently removed from Android L and above... so we are out of luck. Re-write just select pieces of com.android.settings and block the standard settings through MDM - then pray for SYSTEM signature on your app.

Android Intent defaults - detect and clear

Is there a way to detect that a phone has a default application chosen for an intent such as android.intent.action.SEND ?
Is there a way to clear the default application via code?
I'd like to give the user an option to remove the default or at least show a screen telling them how to do it manually, if I can detect it.
Take a look at PackageManager. With it, you can determine how an Intent will be handled with resolveActivity(intent). It looks like the method for clearing the preference (clearPackagePreferredActivities) only works on your own package.
Use 2-step detection of defaults:
PackageManager.queryIntentActivities to get all activities for Intent, PackageManager.resolveActivity to get resolved.
If resolved one is in the list returned by queryIntentActivities, then there will be no "Complete action using" dialog, thus "default" activity was set.

Android Default application

i would like to know how to show
the check box that makes some application the
default in the createChooser dlg.
thanks in advance.
yes David i would like to know how to clear the app from being default. also i would like to know whether i can call any intent if i know its action, category, package and "class name", can I?
I actually tried to call some activity i previously known its intent contents put some SECURITY EXP. raised. why?
Your question isn't 100% clear to me, so forgive me if I'm answering the wrong question. It sounds like you want to either:
Pop up the dialog that lets the user select your activity as the default to handle some intent; or
Check if your activity is the default for an intent.
I'll answer both.
To pop up the dialog that lets the user select your activity as the default to handle some intent, simply call startActivity() with that intent. If for example you wanted to register yourself as the default home screen you would simply:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
1 of 3 things would happen.
Either there is no default specified (and if a user just installed your app, it will clear the default value) and thus the pop up will appear, or you're already the default or someone else is the default.
If you're already the default, then awesome, just figure out how to handle the intent this once (maybe add an EXTRA that explains that this was just a test so your activity knows to finish() immediately).
If someone else is the default you'll need to walk the user through clearing defaults for that app. If no one is the default you may want to throw up a Toast notification that explains what the user should do.
Toast.makeText(this,
"Please check 'Use by default for this action' and then press 'MyActivity'",
Toast.LENGTH_LONG).show();
These are all sort of messy situations to handle and it would be better to check first before calling startActivity(). To do that, use ResolveInfo.
ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
// Got to be a better way to determine if there is no default...
if (res.activityInfo.packageName.equals("android")) {
// No default selected
} else if (res.activityInfo.packageName.equals(getPackageName())) {
// We are default
} else {
// Someone else is default
}
Hope this helps! If you want to know about how to make it easy for the user to clear another app from being default, let me know.

Launching external application from my app

I would like to launch an app the user selects from within my application. However, I'm not sure how I'd go about doing this. I've tried this:
Intent intent = new Intent();
intent.setAction(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
startActivity(intent);
But this seems to throw an error and force close my application. I also tried adding:
<action android:name="Contacts.Intents.SHOW_OR_CREATE_CONTACT"/>
in the AndroidManifest file, but to no avail.
A look at Logcat shows that it's an "IOexception - no such file or directory". A couple of questions arise from this. I read through the Android docs and noticed that the Contact.Intents class is deprecated. However, it's successor, ContactContracts is aimed at API level 5 whereas I'm targeting API level 3. Could this be the problem? Also, I've hardcoded this application into the code. Is there a way to retrieve the intents of any application the user selects so that they can be launched?
You need to pass extra information into the intent to tell Android what you want to show or create. Otherwise Android doesn't know what activity to start and (presumably in your case) throws an ActivityNotFoundException.
For a contact, you use the generic Intent.ACTION_INSERT_OR_EDIT then use the MIME type of an individual contact (Contacts.People.CONTENT_ITEM_TYPE).
For example:
Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
intent.setType(People.CONTENT_ITEM_TYPE);
intent.putExtra(Contacts.Intents.Insert.PHONE, "+1234567890");
intent.putExtra(Contacts.Intents.Insert.PHONE_TYPE, Contacts.PhonesColumns.TYPE_MOBILE);
That will bring up the contacts app, prompting you to select an existing contact to add the phone number to, or to create a new contact.
You don't need to add anything special to your manifest to start external activities. Only if you were to directly manipulate the contacts ContentProvider would you need to add the appropriate CONTACT permissions to your manifest.
I use this code for that purpose:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.android.settings", "com.android.settings.Settings");
startActivity(intent);
This will launch the Settings app, you can use these also:
intent.setClassName("com.android.music", "com.android.music.MediaPlaybackActivityStarter");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity");
intent.setClassName("com.android.contacts", "com.android.contacts.DialtactsActivity");
The first starts the default music app, the second the contacts, and the third the dialer.
Hope this helps.
You need to pass in valid arguments to the apps you start. A lot of apps expect the data URI and / or certain extras to be valid.
Please try the following code:
Intent intent = new Intent(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
this.startActivity(intent);
(sorry if there is something wrong on the syntax, I dont have android in this computer)
And remove the action from the manifest. that is not needed.
The action method is used for something else.
For more info, please look at the android site: http://developer.android.com/reference/android/content/Intent.html
Daniel
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too.

Categories

Resources