In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use Intent to open third-party applications and verify usability. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Basic knowledge
1. The entry Activity of App and its icon
An ordinary application will have an entry Activity by default, which is generally written like this in AndroidManifest.xml:
...
Only if such an Activity is configured, the application will know which Activity to launch when clicking. if you change the value of category to android.intent.category.DEFAULT, the application will not see icon on the desktop and cannot be opened directly.
Use Intent to open third-party applications or specify Activity
Only know the package name-default entry Activity is required
Start the Activity of the specified third-party application-requires the package name and Activity name, and the Export= "true" of the Activity
Implicitly launch third-party applications
1. Use PackageManager.getLaunchIntentForPackage ()
String package_name= "xx.xx.xx"; PackageManager packageManager = context.getPackageManager (); Intent it = packageManager.getLaunchIntentForPackage (package_name); startActivity (it)
This method is aimed at knowing only the package name and is used when you want to start the application. The * restriction on the application is the default entry Activity.
When there is no default entry Activity, a NullPointerException exception is reported:
Java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.toString ()' on a null object reference
Take another look at the description of the getLaunchIntentForPackage () method:
/ * Returns a "good" intent to launch a front-door activity in a package. * This is used, for example, to implement an "open" button when browsing * through packages. The current implementation looks first for a main * activity in the category {@ link Intent#CATEGORY_INFO}, and next for a * main activity in the category {@ link Intent#CATEGORY_LAUNCHER}. Returns * null if neither are found. * * @ param packageName The name of the package to inspect. * * @ return A fully-qualified {@ link Intent} that can be used to launch the * main activity in the package. Returns null if the package * does not contain such an activity, or if packageName is not * recognized. * / public abstract Intent getLaunchIntentForPackage (String packageName)
So use this method to determine whether the Intent is empty.
String package_name = "xx.xx.xx"; PackageManager packageManager = getPackageManager (); Intent it = packageManager.getLaunchIntentForPackage (package_name); if (it! = null) {startActivity (it);} else {/ / has no default entry Activity}
two。 Use Intent.setComponent ()
String package_name = "xx.xx.xx"; String activity_path = "xx.xx.xx.ab.xxActivity"; Intent intent = new Intent (); intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); / / optional ComponentName comp = new ComponentName (package_name,activity_path); intent.setComponent (comp); startActivity (intent)
This method can launch an application-specified Activity, which is not limited to the default entry Activity. However, this method requires many conditions, as follows:
Know the package name of App and the full path and name of Activity
The attribute Export= "true" of the target Activity in AndroidManifest.xml that needs to be started
In this way, how to determine whether the target Activity exists?
The following is a very common usage circulated on the Internet:
String package_name = "xx.xx.xx"; String activity_path = "xx.xx.xx.ab.xxActivity"; Intent intent = new Intent (); intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); / / optional ComponentName cn = new ComponentName (package_name,activity_path); intent.setComponent (cn); if (intent.resolveActivity (getPackageManager ())! = null) {startActivity (intent);} else {/ / specified Activity}
Unfortunately, the Intent.resolveActivity () method does not determine whether the Activity to be started in this way exists, and if the Activity does not exist, it will report a java.lang.IllegalArgumentException: Unknown component exception and cause the program to crash.
Let's take a look at the code for resolveActivity () and its similar method resolveActivityInfo ():
Public ComponentName resolveActivity (PackageManager pm) {if (mComponent! = null) {return mComponent;} ResolveInfo info = pm.resolveActivity (this, PackageManager.MATCH_DEFAULT_ONLY); if (info! = null) {return new ComponentName (info.activityInfo.applicationInfo.packageName, info.activityInfo.name);} return null } public ActivityInfo resolveActivityInfo (PackageManager pm, int flags) {ActivityInfo ai = null; if (mComponent! = null) {try {ai = pm.getActivityInfo (mComponent, flags) } catch (PackageManager.NameNotFoundException e) {/ / ignore}} else {ResolveInfo info = pm.resolveActivity (this, PackageManager.MATCH_DEFAULT_ONLY | flags); if (info! = null) {ai = info.activityInfo;}} return ai;}
Obviously, our way is to set the ComponentName first, so it will be directly return mComponent to us, and there is no logic to determine. On the other hand, resolveActivityInfo () can make a valid decision and return null. Therefore, we choose to use Intent.resolveActivityInfo () to make the decision in this way:
String package_name = "xx.xx.xx"; String activity_path = "xx.xx.xx.ab.xxActivity"; Intent intent = new Intent (); intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); / / optional ComponentName cn = new ComponentName (package_name,activity_path); intent.setComponent (cn); if (intent.resolveActivityInfo (getPackageManager (), PackageManager.MATCH_DEFAULT_ONLY)! = null) {startActivity (intent) } else {/ / cannot find the specified Activity}
3. Implicitly launch third-party applications
This method is mostly used to start functional applications in the system, such as making phone calls, sending emails, previewing pictures, opening a web page using the default browser, and so on.
> Intent intent = new Intent (); > intent.setAction (action); > intent.addCategory (category); > intent.setDataAndType ("abc://www.dfg.com", "image/gif"); > startActivity (intent); >
Conditional 1:IntentFilter has at least one action and at least one Category, and may not have Data and Type
Condition 2: if there is a Data, the Data in the parameter must conform to the Data rule
Conditional 3:Action and Category must match both an Action and a Category in Activity (Category default: android.intent.category.DEFAULT)
There are many implicit startup functions, so they are not enumerated one by one. You can search the relevant code directly when needed. Let's take opening a web page as an example:
Uri uri = Uri.parse ("http://www.abc.xyz"); Intent intent = new Intent (Intent.ACTION_VIEW, uri); startActivity (intent))
At this point, there is nothing wrong with using the Intent.resolveActivity () method directly:
Uri uri = Uri.parse ("http://www.abc.xyz"); Intent intent = new Intent (Intent.ACTION_VIEW, uri); if (intent.resolveActivity (getPackageManager ())! = null) {startActivity (intent);} else {/ / No required applications installed}
Summary
After reading the PackageManager code, it is found that you can also use the packageManager.queryIntentActivities () method to determine whether there is an application in the system that can parse the specified Intent.
Public boolean isAvailable (Context context, Intent intent) {PackageManager packageManager = context.getPackageManager (); List list = packageManager.queryIntentActivities (intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size () > 0;}
So, to sum up:
Method 1: PackageManager.getLaunchIntentForPackage (), which can directly determine whether the returned Intent is empty.
Method 2: Intent.setComponent (), using either Intent.resolveActivityInfo () or packageManager.queryIntentActivities ()
The third way is to start implicitly, using Intent.resolveActivity (), Intent.resolveActivityInfo () and packageManager.queryIntentActivities ().
Thank you for reading! This is the end of the article on "how to use Intent to open third-party applications and verify usability". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.