Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use componentname

Shulou Source: shulou.com Published: 2022-06-03 11:02:00 10月03日 Update

This article mainly introduces how to use componentname, the introduction in the article is very detailed, has a certain reference value, interested friends must read!

ComponentName, as its name implies, is the name of the component. By calling the setComponent method in Intent, we can open an Activity or service in another application.

How to instantiate a ComponentName

Instantiating a ComponentName requires two parameters. The first parameter is the name of the package to start the application, which refers to the package name of the application listed in the manifest file:

The second parameter is the full name of the Activity or Service you want to launch (package name + class name). The code is as follows:

Start an Activity:

Intentintent=newIntent ()

Intent.setComponent (newComponentName ("com.example.otherapp")

"com.example.otherapp.MainActivity2"))

StartActivity (intent)

ComponentName usage

ComponentName: Activity, Service.

ComponentNamechatActivity=newComponentName (param1,param2)

Package name of the application where param1:Activity and Service are located

Package name + class name of param2:Activity and Service

Activity:

ComponentNamechatActivity=newComponentName ("com.npf.chat", "com.npf.chat.ui.ChatActivity")

Intentintent=newIntent ()

Intent.setComponent (chatActivity)

StartActivity (intent)

Service:

ComponentNamechatService=newComponentName ("com.npf.chat", "com.npf.chat.ui.ChatService")

Intentintent=newIntent ()

Intent.setComponent (chatService)

StartService (intent)

Note:

If the Activity is not an application portal (the entry Activity defaults to android:exported= "true"), you need to add android:exported= "true" to the manifest file. Service also needs to add android:exported= "true". Allow external application calls.

The above is all the contents of this article "how to use componentname". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Application name parameter entry content instance file list article class name as the name implies two code value full name interest buddy partner is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia macOS Docker Microsoft