What purpose do different Intent constructors serve?

南楼画角 提交于 2021-02-07 22:41:37

问题


Intent(String action, Uri uri)
Create an intent with a given action and for a given data url.

Intent(Context packageContext, Class<?> cls)
Create an intent for a specific component.

Intent(String action, Uri uri, Context packageContext, Class<?> cls)
Create an intent for a specific component with a specified action and data.

While it may be obvious to some, could you guys help out those of us that have a hard time "getting it"?

For example, in what situation would I want 1 over the other?


回答1:


There are two types of intents:

  • Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.
  • Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

When you create an explicit intent to start an activity or service, the system immediately starts the app component specified in the Intent object.

When you create an implicit intent, the Android system finds the appropriate component to start by comparing the contents of the intent to the intent filters declared in the manifest file of other apps on the device. If the intent matches an intent filter, the system starts that component and delivers it the Intent object. If multiple intent filters are compatible, the system displays a dialog so the user can pick which app to use.

read more at : Intents and Intent Filters


Coming back to your question, all these three constructures are ways to create Implicit/ Explicit intent. Where

  • Intent(String action, Uri uri) creates an implicit intent with given action and uri.

  • Intent(Context packageContext, Class<?> cls) creates an explicit intent.

  • Intent(String action, Uri uri, Context packageContext, Class<?> cls) creates an explicit intent with given action and uri.




回答2:


-use of Intent with "Action" String whenever you want to do some action[Intent.ACTION_DIAL,Intent.ACTION_VIEW]in case of implicit intent. Specifying a broadcastreceiver to broadcast some message. -Intent(Context packageContext, Class cls) when you want to call only new activity from existing activity you can use it.




回答3:


Case 1: asks for an Specific action to be performed(called an impicit intent), I.E.

    Intent(INTENT.ACTION_DIAL,Uri.parse("555-555-5555")) 

The above Dials a phone #,and in some cases, if there is more than one app that can perform the action, it opens up a chooser dialog which could say:

Complete the action with Dialer My_dialer

and awaits user selection.

Case 2: Starts another activity by referencing its name(explicit intent) I.E.

      Intent(Context.this,ActivityToBeLaunched.class)

Case 3:Its a mix of the first two: tells the activity lauched to do something specific I.E.

   Intent(INTENT.ACTION_DIAL,Uri.parse("555-555-5555",Context.this,ActivityToBeLaunched.class))

hope this furthers your understanding in Intents!



来源:https://stackoverflow.com/questions/29026009/what-purpose-do-different-intent-constructors-serve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!