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.
Related
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.
How can I programmatically enable/disable an android screen reader service such as TalkBack?
I am developing a kiosk type application that will be installed on an Android device that will be loaned to visitors while they visit a particular museum. (We are still in the process of determining what device we will use.) The plan is to only allow users to use our app and not have access to the android settings application. However, we'd like to allow users to configure some accessibility settings. When they are finished with the device, we need to restore all the settings to our defaults.
The discussion at the link below has many suggesting launching Android's Settings app. But we don't want users accessing many other settings.
How to Programmatically Enable/Disable Accessibility Service in Android
Only system apps can enable/disable accessibility service programatically.
System apps can directly write in settings secure db to start accessibility service.
Settings.Secure.putString(getContentResolver(),Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "com.packagename/com.packagename.componentname");
Following permission is required to write in settings secure db:
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
For non system apps, only way to start accessibility service is direct them to accessibility settings screen via intent and let user manually start the service :
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
I think there might be a way to do that if you make your app an AccessibilityService (but you would have to enable it manually after install).
Then in your AccessibilityService class, inside onAccessibilityEventmethod you can explore views (recursively) and perform clicks - in the example below it will click on TalkBack item in settings - after that it should toggle the toggle button on next screen (the trick is that you can click on parent not the switch view itself) - I haven't tried this code :)
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if(event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
explore(source);
}
private void explore(AccessibilityNodeInfo view){
int count = view.getChildCount();
for(int i=0; i<count; i++){
AccessibilityNodeInfo child = view.getChild(i);
if(!MODE_TALK_BACK_SCREEN){
if(child.getText()!=null && child.getText().toString().toLowerCase().contains("TalkBack")){
child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
MODE_TALK_BACK_SCREEN=true;
return;
}
}else{
if("ToggleButton".equals(child.getClassName().toString())){ //there ony one toggle button on the screen
child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
performGlobalAction(GLOBAL_ACTION_BACK);
performGlobalAction(GLOBAL_ACTION_BACK);//need to go back two time - i don't know if that will work :)
return;
}
}
explore(child);
child.recycle();
}
so now if you open accessibility settings with Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS); it will perform clicks for you - you would have to somehow cover it with full screen toast or service with view
I'm currently working on automatic airplane mode toggle and it works - so should do the job in your case
take a look on my serviceconfig.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/accessibility_service_description"
android:packageNames="com.android.settings"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>
From lollipop you can not change all settings that breaks the security
policy. Some of them you may access but you have to take permission
for that. So please don't waste time on that.
I'm trying to implement locking the screen with my app.
I've worked my way through google documentation and studied the sample.
When i call this
void getAdmin(){
Intent activateDeviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mPolicy.getPolicyAdmin());
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.AdminReceiverDescr));
startActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
}
The dialogue doesn't show up (maybe it flashes a little, can't say for sure), it just jumps in my onResult routine and result is "not granted". But my app afterwards shows up in the list in settings/security/device admins and if i enable admin rights manually it locks my screen like a charm.
I've added the permission
uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"
as well as
receiver ... android:permission="android.permission.BIND_DEVICE_ADMIN"
to my manifest.
Does anyone have a clue what i may have missed?
Thx in advance!
Found the problem in my manifest: you have to specify the receiver subclass with a $ sign.
Example:
android:name="com.exampl.PolicyClass$PolicyReceiver"
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.
I'm writing a program for the Android Platform and I would like to implement the code of a preexisting application found here .
There is a button in my application menu that says "Show Friends on Map" so I want this program to start from the button press.
For greater detail I will give a small diagram.
User Starts My application > User Presses "Menu" Key > User Presses "Show Friends on Map" > WAMF.apk (the application in the link above) is launched
Is there any way I can do this?
If I understand you correctly and all you want to do is launch WAMF, see this blog post.
In it is the following code, which will detect whether the OpenTable (or WAMF, in this question) is installed, and if so invoke it, otherwise take the user to the Android Market to download OpenTable:
public void showReserveButton() {
// setup the Intent to call OpenTable
Uri reserveUri = Uri.parse(String.format( "reserve://opentable.com/%s?refId=5449",
opentableId));
Intent opentableIntent = new Intent("com.opentable.action.RESERVE", reserveUri);
// setup the Intent to deep link into Android Market
Uri marketUri = Uri.parse("market://search?q=pname:com.opentable");
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
opentableButton.setVisibility(opentableId > 0 ? View.VISIBLE : View.GONE);
opentableButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
PackageManager pm = getPackageManager();
startActivity(pm.queryIntentActivities(opentableIntent, 0).size() == 0 ?
opentableIntent : marketIntent);
}
});
}
As commonsware says, this is assuming that WAMF is available in the Android market. If not, you're out of luck.
(I'm hoping Reto Meier sees your question, as WAMF is his app)
Well, as I see it, you have two main choices.
Option #1 says that WAMF is installed as a separate application. That may be tricky, as it is unclear if this application is available for distribution anywhere (e.g., Android Market). But, assuming it is, and assuming the user has the app installed, when the user invokes your desired menu choice, you need to call startActivity(), using an Intent that will resolve to whatever in WAMF you would like to have displayed. You can also use PackageManager to detect if WAMF is installed (i.e., seeing if there are any activities that would match the Intent you want to use in startActivity()) -- that way, you can disable the menu choice, or have it pop up a dialog telling people to install WAMF, or something.
Option #2 says that, since WAMF is Free Software, you simply integrate the relevant portions of code straight into your app. On the plus side, there's no question whether the code is there. However, should Mr. Meier update the year-old WAMF, you would have to re-integrate his changes. Also, his application is released under GPLv3, which may or may not work with your own app's licensing scheme.