SoFunction
Updated on 2025-03-11

Detailed explanation of how to use Intent to open third-party applications and verify availability in Android development

This article describes the method of using Intent to open third-party applications and verify availability in Android development. Share it for your reference, as follows:

Android provides an Intent mechanism to assist in interaction and communication between applications. Interaction between applications can be accomplished as a medium for communication between different components. Here we discuss the relevant operations of opening third-party applications for Intent.

Main records of this article:

① Three ways to use Intent to open a third-party application or specify an activity

② How to determine whether the Intent can be parsed when using the above three methods

③ Determine whether the intent can be parsed and may appear in the omissions

Basic knowledge

1. The entrance to the app Activity and its icon

An ordinary application will have an entry activity by default, which is generally written like this in:

<application>
  <activity android:name=".MainActivity" >
    <intent-filter>
      <action android:name="" />
      <category android:name="" />
    </intent-filter>
  </activity>
  ...
</application>

Only when such an activity is configured can the application know which activity to start when clicking.category The value ofThen, this application will not be able to see the icon on the desktop and cannot be opened directly.

How to open a third-party app or specify an activity using Intent

① Only know the package name - there is a default entry Activity

② Start the Activity of the specified third-party application - requires the package name and Activity name, and the ActivityExport="true"

③ Implicitly launch third-party applications

1. Use()

String package_name="";
PackageManager packageManager = ();
Intent it = (package_name);
startActivity(it);

This method is for only knowing the package name and using it when you want to start the application. The only limitation on the application is to have a default entry activity.

When there is no default entry Activity, a NullPointerException exception will be reported:

: Attempt to invoke virtual method ' ()' on a null object reference

Let's take a 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
* <code>null</code> 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 <code>null</code> if the package
* does not contain such an activity, or if <em>packageName</em> is not
* recognized.
*/
public abstract Intent getLaunchIntentForPackage(String packageName);

So use this method to determine whether the Intent is empty.

String package_name = "";
PackageManager packageManager = getPackageManager();
Intent it = (package_name);
if (it != null){
  startActivity(it);
}else{
  //No default entry Activity}

2. Use()

String package_name = "";
String activity_path = "";
Intent intent = new Intent();
(Intent.FLAG_ACTIVITY_NEW_TASK);//OptionalComponentName comp = new ComponentName(package_name,activity_path);
(comp);
startActivity(intent);

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 app's package name and the full path of the activity and its name

Attributes of the target Activity that needs to be started inExport="true"

So in this way, how to judge whether the target activity exists?

Here are the very common usages circulated online:

String package_name = "";
String activity_path = "";
Intent intent = new Intent();
(Intent.FLAG_ACTIVITY_NEW_TASK);//OptionalComponentName cn = new ComponentName(package_name,activity_path);
(cn);
if ((getPackageManager()) != null) {
  startActivity(intent);
} else {
  //The specified activity cannot be found}

Unfortunately,()The method cannot determine whether the activity to be started by this method exists. If this activity does not exist, it will be reported.: Unknown component Exception and causes the program to crash.

See belowresolveActivity()code, and its similar methodsresolveActivityInfo()

public ComponentName resolveActivity(PackageManager pm) {
  if (mComponent != null) {
    return mComponent;
  }
  ResolveInfo info = (this,
    PackageManager.MATCH_DEFAULT_ONLY);
  if (info != null) {
    return new ComponentName(
      ,
      );
  }
  return null;
}
public ActivityInfo resolveActivityInfo(PackageManager pm, int flags) {
  ActivityInfo ai = null;
  if (mComponent != null) {
    try {
      ai = (mComponent, flags);
    } catch ( e) {
      // ignore
    }
  } else {
    ResolveInfo info = (this,
      PackageManager.MATCH_DEFAULT_ONLY | flags);
    if (info != null) {
      ai = ;
    }
  }
  return ai;
}

Obviously, our method is to set the ComponentName first, so it will be directlyreturn mComponentGiven to us, there is no logic for judgment. Relatively,resolveActivityInfo()Then valid judgment can be made and null will be returned. Therefore, we choose to use()Make a judgment under this method:

String package_name = "";
String activity_path = "";
Intent intent = new Intent();
(Intent.FLAG_ACTIVITY_NEW_TASK);//OptionalComponentName cn = new ComponentName(package_name,activity_path);
(cn);
if ((getPackageManager(), PackageManager.MATCH_DEFAULT_ONLY) != null) {
  startActivity(intent);
} else {
  //The specified activity cannot be found}

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, using the default browser to open a web page, etc.

Intent intent = new Intent();
(action);
(category);
("abc://","image/gif");
startActivity(intent);

Condition 1: IntentFilter has at least one action, at least one Category, and can be without 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 both an Action and a Category in the Activity (Category default:)

There are many implicit startup functions, so I won’t list them one by one. Just search for the relevant code directly when needed. Let’s use opening a web page as an example:

Uri uri = ("");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

At this time, use it directly()There is no problem with the method:

Uri uri = ("");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if ((getPackageManager()) != null) {
  startActivity(intent);
} else {
  // No application required is installed}

Summarize

After reading the PackageManager code, I found that it can still be used()Method determines whether there is an application in the system that can parse the specified Intent.

public boolean isAvailable(Context context, Intent intent) {
  PackageManager packageManager = ();
  List list = (intent,
  PackageManager.MATCH_DEFAULT_ONLY);
  return () > 0;
}

So, to summarize it:

Method one(), just directly determine whether the returned Intent is empty;

Method 2(),use()or()Two ways;

Method 3: Implicit startup, use()()()All three methods are possible.

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.