Android Intent defaults - detect and clear - android

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.

Related

Opening Do Not Disturb preferences in Oreo

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.

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.

re-prompt to choose default activity

is there a way to reprompt the user to choose a default activity for an intent? For example, user selects his default home apk and I want him to reconsider his choice once again.
I know how to do that on 2.1 and before, but is there a way to do that now on 2.2?
Famous Home Switcher, which did similar thing, does not work on 2.2 anymore thanks to google team
This is how I represent the Activity selection dialog:
It start the android default ResolverActivity for "HOME" Applications.
Intent selector = new Intent("android.intent.action.MAIN");
selector.addCategory("android.intent.category.HOME");
selector.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
startActivity(selector);
The above code is working for my 2.2 enabled tablets.
When executed, it displays the "Complete Actions with:" dialog with all possible Home applications in the list.
A way to detect which is currently set by default you could ask for all preferred activities. The lists "filters" and "comps" contain the data when calling .getPreferredActivities(...).
filters - contains the intent filter data, which you could query what type of data it is.
comps - contians the component which would be called if the intent filter matches
This way you could check if your application is the current "home" application set as preferred by the user.
List<IntentFilter> filters = new ArrayList<IntentFilter>();
List<ComponentName> comps= new ArrayList<ComponentName>();
getPackageManager().getPreferredActivities(filters, comps, null);
For example, user selects his default home apk and I want him to reconsider his choice once again.
That is no longer possible, unless your app is the preferred one. Then, I think you can use clearPackagePreferredActivities() to remove yourself as the preferred choice.
In other words, you are welcome to affect your own app, but you are not welcome to affect other apps.

Receive explicit intent. Android

Is it possible to receive an explicit intent with the component option set?
Example:
Starting activity: Intent { action=android.intent.action.VIEW data=http://example.org flags=0x10000000 comp={com.android.browser/com.android.browser.BrowserActivity} }
The reason why i want to this this is because i want receive this intent above, launch another browser than the default one, and drop the intent before it starts the default browser. In another words, i want to make another browser the default one.
If this is impossible, any other ideas how i can accomplish this?
Thanks!
1) You can explicitly launch alternative browser by calling something like startActivity(new Intent(this, OtherBrowser.class)) from Activity.
2) You can't override the default browser, but you can provide a browser alternative that user could choose when opening http://something. Just have a look at intent-filters that the default Browser declares (from Android sources).

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