I am looking to start a specific application(Dictionary app) from my app. Using intents, how would I go about launching that specific app and use it to look up the word.
There are implicit intents and explicit intents, you want an explicit intent to get your desirable.
Here is how you can do it.
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);
in the setComponent method your Dictionary app information should go in.
CommonsWare addition to this answer,
Using an explicit Intent to talk to a third-party app is rarely the right thing to do. This code will break if the activity is not exported, or requires permissions, or the developer refactors the code and changes the class name or package, etc.
If the author of the app is documenting that your recipe is the correct way to work with that activity on that app, then that is fine, as the developer presumably intends to support this use case.
Related
In my code I want to trigger an implicit intent to open another android library activity.
Intent i = new Intent("Shared library", Uri.parse("https://www.google.com/"));
startActivity(i);
Lets say few apps contain this same library. If I trigger an implicit intent will the user see the "select an app to handle this intent"?
Or because all the apps contain the same library it will just open?
If I trigger an implicit intent will the user see the "select an app to handle this intent"?
If the library:
...is an AAR
...has an AndroidManifest.xml file, which...
...has an <activity> with an <intent-filter> that matches your implicit Intent
Then any app that incorporates the library and is not blocking that <activity> via its own manifest will be a candidate for responding to that implicit Intent. If there are 2+ of those apps, normal Android implicit Intent resolution occurs:
If the user set a default for this Intent structure on a previous startActivity() call, that default activity will be the one that is started
Otherwise, the user will see a chooser
Or because all the apps contain the same library it will just open?
No, because Android does not know which app is the one that the user wants.
How can i use implicit intent but avoid “select an app to handle this intent” screen?
You can't, if the user has not set a default activity. It is up to the user to decide which app's activity should handle that implicit Intent.
To put it another way: malware authors would love the ability to unilaterally hijack implicit Intents and force them all to route to the malware.
is it possible to call more packages in single project,without installing the other packages in emulator/phone,want single apk file..I Kept this code...but is possible when the package is available in emulator/phone..Please suggest me...if that package is not available in emulator/phone
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName(
"com.abc.def.packname",
"com.abc.def.packname.MyActivity"));
startActivity(intent);
To be able to use an Intent to launch any Activity, there must be an Activity available to Handle it. This applies for both direct intents that target one particular Activity, and more general intents which would result in a Chooser dialog, like the share intent.
So in short, yes, the package you are referencing must be installed as well, or else your app will crash. If you have access to the source code of the other project, you could combine them into one. If this isn't possible, you could request that the user installs the other app when your app starts.
I have to attach an image file with the sms on button click i use this code
final Intent smsIntent = new Intent(android.content.Intent.ACTION_SEND);
smsIntent.putExtra("sms_body", "Hello World!");
smsIntent.putExtra("address", "0123456789");
smsIntent.putExtra(Intent.ACTION_ATTACH_DATA,screenshotUri);
smsIntent.setType("image/png");
startActivity(smsIntent);
But this shows a chooser to choose an action like Facebook,Email,Messages etc.
But i do not want any the chosser view it will directly show the message intent with attached file.
What you are looking for cannot be done using implicit Intents. Because that is how the Android system is designed to handle implicit intents. If you want your intent to be handled by a specific application then you have to make them explicit, i.e., specify a component that has to handle the intent. But when you use explicit intents to handle any situation such as yours, then there is a great chance of your app to break when the specific component (i.e. Application) doesn't not exist in the target device. Android is being adopted by several OEMs, therefore each of them tend to replace the stock Messaging application with their own. So what seems to work on one device may not work on the other.
If you want to achieve what you want then you may have to get the list of Messaging applications on various devices, (you can find the stock Android app's component name from the emulator itself). And use the PackageManager to find if the component exists. If it does, the start and explicit intent, in which you won't receive an IntentChooser. If the component doesn't exist send an implicit intent.
You can learn more about intents from here.
Determining if an Activity exists on the current device? - This post will help you to find if the target component exists.
My question is, is it possible to call one app from another?
It would be very helpful if anyone had an answer or solution.
-Chris-
Yes, by using intents.
For example:
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.setClassName("com.example.theotherapp", "com.example.theotherapp.MainActivity");
startActivity(intent);
This is called an explicit intent, because you're explicitly stating which component should respond to it. You can also use implicit intents, in which you specify what kind of component you expect and the OS and/or the user selects the most appropriate one.
If you can choose, implicit intents are preferred.
You should take a look at http://developer.android.com/guide/topics/fundamentals.html, more specifically at the "Application Components" section.
There are many ways to make two applications talk to each other - and they're explained there.
My app needs to have a intent-filter that responds to a Intent that has it's component set (a explicit intent.) Here is a example.
Intent i = new Intent();
i.setClassName("com.compareeverywhere","com.compareeverywhere.ScanActivity");
startActivity(i);
Just a simple intent-filter will not do - because the Intent is made for a specific component (Activity,) it just launches that without looking for intents at all. Is there a way to do this?
Thanks, Isaac Waller
P.S: Please don't reply "No."
No.
:) That being said, imagine what would happen if Android allowed people to hijack Intents for specific components. Don't like a competitor's app? Just have yours hijack his main Activity with your own to display porn. Intents can specify specific components specifically because the authors don't want others to be able to replace them.
You have two options. If this is your own code, replace it with a generic intent, or if it belongs to someone else, contact them, and ask nicely for them to change it to a generic intent along with some good reasons why that is necessary.