This article mainly records:
Basics 1. App entry Activity and its icon
A normal application will have an entry Activity by default, which is usually written in AndroidManifest.xml as follows:
Only when such an Activity is configured, the application will know which Activity to start when it is clicked. If the value of category is changed to android.intent.category.DEFAULT, then the icon of this application will not be visible on the desktop and it cannot be opened directly. How to use Intent to open a third-party application or specify an Activity Only know the package name - need to have a default entry Activity Start the Activity of a specified third-party application - the package name and Activity name are required, and the Activity's Export="true" Implicitly launch third-party applications 1. Use PackageManager.getLaunchIntentForPackage()
This method is used when you only know the package name and want to start the application. The biggest restriction on the application is that there is a default entry Activity. When there is no default entry Activity, a NullPointerException will be reported:
Let’s take a look at the description of the getLaunchIntentForPackage() method:
So you can use this method to determine whether the Intent is empty.
2. Use Intent.setComponent()
This method can start an application-specified Activity, not limited to the default entry Activity. However, this method requires many conditions, as follows: Know the package name of the App and the full path and name of the Activity The target Activity to be started has the attribute Export="true" in AndroidManifest.xml In this way, how to determine whether the target Activity exists? The following is a very common usage circulating on the Internet:
Unfortunately, the Intent.resolveActivity() method cannot determine whether the Activity to be started in this way exists. If this Activity does not exist, a java.lang.IllegalArgumentException: Unknown component exception will be reported, causing the program to crash. Let's look at the code for resolveActivity() and its similar method resolveActivityInfo():
Obviously, in this method, we set the ComponentName first, so mComponent will be returned directly to us without any judgment logic. In contrast, resolveActivityInfo() can make effective judgments and return null. Therefore, we choose to use Intent.resolveActivityInfo() to make judgments in this way:
3. Implicitly launch third-party applications This method is mostly used to start functional applications in the system, such as making calls, sending emails, previewing pictures, opening a web page using the default browser, etc.
Condition 1: IntentFilter has at least one action and at least one Category, but may not have Data and Type Condition 2: If there is data, the data in the parameter must comply with the data rules Condition 3: Action and Category must match an Action and a Category in the Activity (Category default: android.intent.category.DEFAULT) There are many implicit startup functions, so I won’t list them all. You can directly search for relevant codes when needed. Let’s take opening a web page as an example:
At this point, there is nothing wrong with using the Intent.resolveActivity() method directly:
Summarize After reading the PackageManager code, I 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.
So, to sum up: Method 1: PackageManager.getLaunchIntentForPackage(), directly determine whether the returned Intent is empty; Method 2: Intent.setComponent(), use Intent.resolveActivityInfo() or packageManager.queryIntentActivities(); Method 3: Implicit start, using Intent.resolveActivity(), Intent.resolveActivityInfo(), packageManager.queryIntentActivities(). |
<<: AR/VR experts tell you: What will the future of human-computer interaction look like?
>>: Droidcon Beijing 2017 Android Technology Conference will be held in November
This article mainly introduces what kind of posts...
As an old technical person, I will not talk about...
This article was reviewed by Zhu Hongjian, Chief ...
Editor's note: The author of this article, Ni...
We all know that it is difficult for plants to gr...
As the Spring Festival approaches, many companies...
User activation is the first step after completin...
JD.com and Taobao have been competing fiercely du...
It has been more than 4 years since the WeChat Mi...
This article was reviewed by Pa Li Ze, chief phys...
Lollipop system encryption options According to f...
Source: People's Daily The cover image and th...
News reports that my country's oil and gas dr...
The smartphone market has entered a stage of fier...
In the past two years, private domain traffic has...