How to open other apps in Android

Many times, we want to open a third-party application. For example, in an advertisement, we can determine whether the application is installed. If it is installed, it will jump to the homepage of the application. If it is not installed, it will go to the application details page in the app store.
Then let’s talk about several solutions for opening the homepage of third-party applications.

Jump by package name

Know that there are three types of package name jumps:

a.通过packageManager.getLaunchIntentForPackage(packageName: String)

code show as below

 fun startAppWithPackageName(context: Context, packageName: String) {
    
    
 	//根据包名获取启动首页的intent 这个intent有可能为空
        val intent = context.packageManager.getLaunchIntentForPackage(packageName)
        if (intent != null) {
    
    
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }
 }

Looking at the method name, you can know that the intent here is to obtain the class whose category is Launch in the package name through PackageManager. If such a class is not defined in an application, the naturally obtained intent will be empty. At this point, let’s expand
:

If all activities in the manifest file of an application do not have
<category android:name="android.intent.category.LAUNCHER" />
, then the application icon will be hidden on the mobile desktop. Of course, the following settings must be set in the main activity <intent-filter>

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

b. Also know the specific class name of the activity in the application

fun startAppWithPackageName(context: Context, packageName: String, activityName: String) {
    
    
	//activityName 举例:com.google.app.MainActivity
	val intent = Intent().apply {
    
    
            component = ComponentName(packageName, activityName)
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
        }
        context.startActivity(intent)
}

c. Jump through the scheme in Uri

When you know the package name and scheme of a third-party application, you can open the application through the following scheme. The
code is as follows:

 val intent = Intent().apply {
    
    
      data = Uri.parse("scheme://authorty?query")
      setPackage(packageName)
      setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
 }
 context.startActivity(intent)

Points to note here:
The scheme must be declared in the third-party application. If multiple third-party applications set the same scheme, you must set setPackage(packageName) if you want to jump to the specified application.

Expand it

If you are careful, you will find that I have set setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) for the above three startup methods.

So what is the function of this flag?

In fact, this flag is more commonly used in activity jumps, which means that if the class exists in the stack, the class will be started directly in the stack, otherwise a new stack will be created to start the class.

Then this flag also has the same function when starting the application: treat the entire Android system as an app, in which different apps have different activities. The difference is that if the app has already been started, using this flag will only call up The app, if the app is not started, then the app will be started.

Then if we start an app, we will pass a lot of parameters. Generally, the app will receive and process these parameters on the startup page. Then we use the above FLAG_ACTIVITY_NEW_TASK. When the application is alive, it will only wake up the app and not go to the startup page. Then we How to restart the application?

Intent.FLAG_ACTIVITY_CLEAR_TASK will work. We can restart the application by setting the following at startup.

setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)

Note: Intent.FLAG_ACTIVITY_CLEAR_TASK is only valid if set at the same time as Intent.FLAG_ACTIVITY_NEW_TASK.

However, some applications may intercept this restart event on the startup page, so the following actions must be set, and this depends on the processing solution of the third-party application.
The judgment of intercepting restart is:

intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0

end

Currently, we only know the above three solutions for launching new applications. If there are any others, please point them out in the comments and let us make progress together.

Guess you like

Origin blog.csdn.net/qq_39734865/article/details/131751983