When you do startActivity() with chooser, Android would list all apps entitled to handle your Intent along with options to set this assignment permanent or once-time (on ICS its Always and Just once action button, on 2.x it's a checkbox). However for this code:
public class Redirector {
public static void showActivityWithChooser( Context context, int chooserLabelTitleId, Intent intent ) {
try {
context.startActivity( Intent.createChooser( intent,
context.getResources().getString( chooserLabelTitleId )) );
} catch( Exception e ) {
e.printStackTrace();
}
}
public static void viewInExternalApplication( Context context, String url ) {
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setData( Uri.parse( url ) );
intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
showActivityWithChooser( context, R.string.open_chooser_title, intent );
}
}
I see no Always | Just once buttons and cannot make my selection permanent. What elementary I overlooked that made Android unable to make user choice persistent?
See the pics: left dialog is what I'd like to see, but right is what I get now (different number of applications in both dialogs is irrelevant):
For a record - it was over-interpretation type of bug (of mine). The chooser I was using is exactly what can be seen on the image on the right side. And it was showing up all the time because... I was calling it all the time. I incorrectly assumed that chooser offers Always | Just once functionality and would not show up if user tapped "Always" (and will show up if s/he used Just once). But it is wrong. Chooser will always show up because that's its role - to let user choose.
The Always | Just once functionality is different thing -> it is a feature of the Android framework for startActivity() (and startActivityForResult() etc) and it would show up automatically when needed (when there's more than one app, or precisely, more than one matching intent-filter declared in app manifest, that matches certain Intent). It would not show up if you got just one (or if user already choose Always).
So you, as developer do not need to care nor handle the case in any special way. To fix the issue I simply changed my viewInExternalApplication() code to just call startActivity():
try {
context.startActivity( intent );
} catch (.... )
and let the framework do the rest.
Related
So I'm just wondering why my code isn't working. How do I give AppShorcutIntent a specific intent with an action and data and stuff like that?
This is my code so far:
val appShortcutIntent = AppShortcutIntent.builder()
.setIntentName("actions.intent.OPEN_APP_FEATURE")
.setPackageName("com.app.name")
.setIntentParamName("feature")
.setIntentParamValue("")
.build()
shortcutsClient.lookupShortcut(appShortcutIntent)
.addOnSuccessListener { shortcutLookupResult ->
if (shortcutLookupResult.isShortcutPresent) {
shortcutsClient.createShortcutSettingsIntent().addOnSuccessListener { intent ->
requireActivity().startActivity(intent)
}
return#addOnSuccessListener
}
val signalShortcut = AppShortcutSuggestion.builder()
.setAppShortcutIntent(appShortcutIntent)
.setCommand("feature on")
.build()
shortcutsClient.createShortcutSuggestionIntent(signalShortcut).addOnSuccessListener { intent ->
requireActivity().startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}
}
I have tried so many different things and none of it seems to want to work the way I want. I know the question doesn't have anything specific as the parameter value but no matter what I set the param value too it still just doesn't get recognized as a unique intent when I use the shortcut.
The in-app promo library API doesn't deal with Android intents. It deals with Assistant's built-in intents, which are an entirely different things (even though they are both called "intents"). In the example you copied, it refers to the BII called OPEN_APP_FEATURE.
By using this API, you are telling Assistant how to create a shortcut that launches the app using a BII that it is already configured to handle. This BII is important because it carries the ability to recognize natural language queries associated with it. Android intents don't have that context.
I'm currently developing an Android application in order to display home screen widgets. Those ones are related to Microsoft Outlook (Events + Messages) in order to show incoming events and unread new messages in a kind of dynamic tiles.
The Msal graph library helps me a lot to authenticate and retrieve in formations which contains an identifier for each event / message results
But now I want to know if the outlook application is installed on the user device and if there is a way to open Outlook when the user click on the widget. Moreover if the user can open the corresponding clicked event or message with the identifier.
For example the Event widget currently displaying a birthday event. The user click on it. Then it opens Outlook and display directly that birthday event.
Regards
I don't think this is officially documented somewhere. But here's what you can do to find out about it.
You can list all Microsoft applications installed on your device...
val packages = context.packageManager
.getInstalledApplications(PackageManager.GET_META_DATA)
for (info in packages) {
if(info.packageName.startsWith("com.microsoft", true)){
Log.d("package name:" + info.packageName)
Log.d("Launch Activity: " + context.packageManager.getLaunchIntentForPackage(info.packageName))
}
}
Take a note of the "launch intent" displayed in the LogCat. You can use that to launch Outlook. Just make sure you don't hard-code those values because Microsoft can change those values at any point, for example the activity class can change. So, instead of doing this...
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
setPackage("com.microsoft.office.outlook")
component = ComponentName("com.microsoft.office.outlook", "com.microsoft.office.outlook.MainActivity")
}
)
Do this...
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName(
outlookLaunchIntent?.component?.packageName,
outlookLaunchIntent?.component?.className
)
setPackage(outlookLaunchIntent.package)
}
)
Also, remember that getLaunchIntentForPackage and component can return null, so make sure you check for null values properly
I am relaying a suggestion from a couple of internal folks:
Please try to open the event using one of the following URLs:
ms-outlook://events/open?restid=%s&account=test#om.com (if you have a regular REST id)
ms-outlook://events/open?immutableid=%s&account=test#om.com (if you are using an immutable id)
Since immutable IDs are still in preview stage in Microsoft Graph, and customers should not use preview APIs in their production apps, I think option #1 applies to your case.
Please reply here if the URL works, or not, and if you have other related questions. I requested the couple of folks to keep an eye on this thread as well.
Well, i managed to open the outlook android application with the help of your code #Leo. As im not developping with Kotlin, ill post the JAVA code below :
Intent outlookLaunchIntent = context.getPackageManager().getLaunchIntentForPackage("com.microsoft.office.outlook");
if (outlookLaunchIntent != null) {
context.startActivity(outlookLaunchIntent );
}
Below code to open event/message in a web browser provided by webLink property of the graph API. (I only test for event and the url provided not working. Ill post a new issue on StackOverFlow for that but you already see the issue over there : https://github.com/microsoftgraph/microsoft-graph-docs/issues/4203
try {
Intent webIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(calendarWebLink));
webIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(webIntent);
} catch (RuntimeException e) {
// The url is invalid, maybe missing http://
e.printStackTrace();
}
However im still stuck on the decicive goal of my widget item click which is to open the relative event/email in the Microsoft Outlook Android application.
Microsoft Outlook Android app contains widgets which can achieve what im looking for. So i wonder if it is possible to list its broadcast receivers.
The best thing i found is an old manifest for that app but it doesnt help me.
https://gist.github.com/RyPope/df0e61f477af4b73865cd72bdaa7d8c2
Hi may you try to open the event using one of the url:
ms-outlook://events/open?restid=%s&account=test#om.com (If the
user is having rest id)
ms-outlook://events/open?immutableid=%s&account=test#om.com (If
the user is having immutable id)
I'm trying to use enableSystemApp method to activate default system apps after provisioning device with the app that is set to device owner mode.
There are two methods to do this:
1) void enableSystemApp (ComponentName admin, String packageName) - in this case you need to pass package name explicitly as String. It works fine, the app gets enabled.
For example, calling this
devicePolicyManager.enableSystemApp(deviceAdminComponent, "com.google.android.gm");
enables default Gmail client, which is disabled after provisioning.
2) int enableSystemApp (ComponentName admin, Intent intent) - in this case, you need to pass an implicit intent and Android should enable all system apps that match this intent. In addition, this method returns int number of apps that match the intent. And here's the problem - I can't get this method to work, it always returns 0 and doesn't enable anything.
Here's the snippet I'm trying to use:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
int i = devicePolicyManager.enableSystemApp(deviceAdminComponent, intent);
It does not work and i == 0 in this case. What am I doing wrong?
Any help is appreciated!
Under the hood, the method that accepts an intent queries to get the list of activities that respond to that intent and then loops through the list passing in the package name string to enable the package. It's similar to doing this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
List<ResolveInfo> infoes = getPackageManager()
.queryIntentActivities(intent, MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE);
for (ResolveInfo info in infoes) {
devicePolicyManager.enableSystemApp(deviceAdminComponent, info.activityInfo.packageName);
}
Since you are able to enable the app using the package name string, the fault most likely lies in the way the intent is being resolved - which is supported by the fact that it always returns 0.
It is counter-intuitive, but my suspicion is that the application does not resolve the ACTION_MAIN intent because the app is disabled. Have you tried a less generic intent? I would try the following
Intent i;
// #1
// This goes full circle, but I expect it should work
i = getPackageManager().getLaunchIntentForPackage("com.google.android.gm")
// #2
i = new Intent(Intent.ACTION_SEND).setPackageName("com.google.android.gm");
// #3
// Generic, but should resolve _all_ email apps - not just the default one.
// The mailto schema filters out non-email apps
i = new Intent(Intent.ACTION_VIEW , Uri.parse("mailto:"));
Option #1 and #2 are more academic. Both require the package name at which point you may as well use the string overload of enableSystemApp. Option #3 is my best guess for something generic that might still work, but it's possible that it still won't work because the app is disabled.
Note: I find it interesting that enableSystemApp only passes the MATCH_DIRECT_BOOT_AWARE and MATCH_DIRECT_BOOT_UNAWARE flags when querying activities that can resolve the intent, because the MATCH_DISABLED_COMPONENTS and MATCH_SYSTEM_ONLY flags seem much more relevant in this situation.
I am currently trying to implement a custom keyboard that sends an image (possibly via an intent?) to a certain application. More specifically, I am trying to I am trying to create a key on a custom keyboard that would send an image to the default messaging app so that it can be sent as an MMS.
I have modified the sample SoftKeyboard project in order to do this. Here is what I have so far:
private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
if (mInputView.isShifted()) {
primaryCode = Character.toUpperCase(primaryCode);
}
}
if (isAlphabet(primaryCode) && mPredictionOn) {
mComposing.append((char) primaryCode);
// Send message here
Intent pictureMessageIntent = new Intent(Intent.ACTION_SEND);
pictureMessageIntent.setType("image/png");
Uri uri = Uri.parse("android.resource://" + this.getPackageName() + "/drawable/icon_en_gb");
pictureMessageIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(pictureMessageIntent);
getCurrentInputConnection().setComposingText(mComposing, 1);
updateShiftKeyState(getCurrentInputEditorInfo());
updateCandidates();
} else {
getCurrentInputConnection().commitText(
String.valueOf((char) primaryCode), 1);
}
}
The problem is I am getting a runtime exception that says:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I'm unfamiliar with Android custom keyboards, but I'm not sure if starting a new Activity is the best idea. Does anyone have any suggestions?
The problem is that Activities are typically meant to be started from other Activities. Android added this error to make sure that developers only change this flow willingly and consciously.
Add the following line before sending the intent to fix the problem:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
You need to do exactly what it says- add the FLAFG_ACTIVITY_NEW_TASK flag to the intent. Any intent from a service needs that. Fix that and it should work (or at least start the activity).
Put this line:
pictureMessageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
as you are creating this keyboard for sending images from uri, then add this line too:
// To get images from uri
pictureMessageIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Before the line:
startActivity(pictureMessageIntent);
first off - let me just say that I am NOT asking how to implement a share button in my app or anything like that. I know all about using Intents and Intent Filters etc etc.
what I AM asking about is this: is there any way to get access to the "Share" menu itself? in other words, I'd love to build an app that filters out some of the services I never use but that I don't want to delete from my phone completely.
I tried looking it up in the Android API, but only found info on getting your app to show up in the menu or putting a 'Share' button in your app etc.
Being that I'm still somewhat of a novice programmer, I'm also wondering if there's some way for me to sniff out the API objects that are being created/used when the 'Share' menu is built/displayed? Seems like I could do it in a Debugger session, but I'm not sure how.
Thank you in advance.
b
Well, there are two ways to go around Share menu. First one is to use
startActivity(Intent.createChooser(Intent, CharSequence)
But in this case, I am not sure how to obtain an access to the created share menu, coz it is a separate activity.
However, if you wish to have a control over the list of share items being displayed for your app, there is another way to approach your share menu item implementation.
Take a look at this code snippet:
//Prepare an intent to filter the activities you need
//Add a List<YourItemType> where you going to store the share-items
List<YourItemType> myShareList = new List<YourItemType>;
PackageManager packageManager = mContext.getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
int numActivities = activities.size();
for (int i = 0; i != numActivities; ++i) {
final ResolveInfo info = activities.get(i);
String label = info.loadLabel(packageManager).toString();
//now you can check label or some other info and decide whether to add the item
//into your own list of share items
//Every item in your list should have a runnable which will execute
// proper share-action (Activity)
myShareList.add(new YourItemType(label, info.loadIcon(packageManager), new Runnable()
{
public void run() {
startResolvedActivity(intent, info);
}
}));
}
This code snippet shows how to get a list of the activities which are able to process share request. What you need to do next is to show your own UI. It is up to you what you are going to choose.