What is the use of Android Intent action ???

Source: Internet
Author: User

Instance 1:

There are three activities: MainActivity. java/BAcitvity. java/CActivity. java

Assume that this is a photo app MainActivity, which is the program entry. Both BActivity and CActivity can process the photo content.

When we do not use action, how can we enable this application to use both BActivity photography and CActivity photography?

Of course, two buttons are written in MainActivity,

New Intent (MainActivity. this, BActivity. class );

New Intent (MainActivity. this, CActivity. class );

Two different intents are used to initiate a request.

 


So how to use action to implement it?

In xml, we first define the following:


[Html]

<Activity
Android: name = ". MainActivity"
Android: label = "@ string/title_activity_main">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
 
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
 
<Activity
Android: name = ". BActivity"
Android: label = "@ string/title_activity_main"
Android: launchMode = "singleTask">
<Intent-filter>
<Action android: name = "com. h3c. intent. ACTION_VIEW"> </action>
<Category android: name = "android. intent. category. DEFAULT"> </category>
</Intent-filter>
</Activity>
 
<Activity
Android: name = ". CActivity"
Android: label = "@ string/title_activity_main">
<Intent-filter>
<Action android: name = "com. h3c. intent. ACTION_VIEW"> </action>
<Category android: name = "android. intent. category. DEFAULT"> </category>
</Intent-filter>
</Activity>
<Activity
Android: name = ". MainActivity"
Android: label = "@ string/title_activity_main">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>

<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>

<Activity
Android: name = ". BActivity"
Android: label = "@ string/title_activity_main"
Android: launchMode = "singleTask">
<Intent-filter>
<Action android: name = "com. h3c. intent. ACTION_VIEW"> </action>
<Category android: name = "android. intent. category. DEFAULT"> </category>
</Intent-filter>
</Activity>

<Activity
Android: name = ". CActivity"
Android: label = "@ string/title_activity_main">
<Intent-filter>
<Action android: name = "com. h3c. intent. ACTION_VIEW"> </action>
<Category android: name = "android. intent. category. DEFAULT"> </category>
</Intent-filter>
</Activity>
We implement a Button in MainActivity and click send:

New Intent ("com. h3c. intent. ACTION_VIEW ");

In this case, a Dialog will pop up, so that we can automatically choose whether to use BActivity or CActiviy, as shown in the example of camer360 I mentioned above.

This is the implicit intent, that is, to send an action type, so that all activities that support this type can receive a message, and then the user selects which activity to use for processing.

 


Example 2: (Android sharing and receiving)

I have actually touched on action before, but I have no idea about it myself. For example, how can we implement the DLNA project by supporting third-party media apps to share with DLNA?

First, let's take a look at how to share it with others:


[Java]
Public static void shareText (Context context, String title, String text ){
Intent intent = new Intent (Intent. ACTION_SEND );
Intent. setType ("text/plain ");
Intent. putExtra (Intent. EXTRA_SUBJECT, title );
Intent. putExtra (Intent. EXTRA_TEXT, text );
Context. startActivity (Intent. createChooser (intent, title ));
}
Public static void shareText (Context context, String title, String text ){
Intent intent = new Intent (Intent. ACTION_SEND );
Intent. setType ("text/plain ");
Intent. putExtra (Intent. EXTRA_SUBJECT, title );
Intent. putExtra (Intent. EXTRA_TEXT, text );
Context. startActivity (Intent. createChooser (intent, title ));
} Is to use the implicit intent mentioned above, because I don't know which applications can support sharing, so I use the implicit intent to initiate sharing requests.

 


Let's take a look at how to support sharing:

[Html]
<Activity android: name = ". SharePage" android: label = "share to Weibo">
<Intent-filter>
<Action android: name = "android. intent. action. SEND"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Data android: mimeType = "image/*"/>
</Intent-filter>
</Activity>
<Activity android: name = ". SharePage" android: label = "share to Weibo">
<Intent-filter>
<Action android: name = "android. intent. action. SEND"/>
<Category android: name = "android. intent. category. DEFAULT"/>
<Data android: mimeType = "image/*"/>
</Intent-filter>
</Activity> yes. Use action to tell others that I support the SEND implicit intent class. Note that there must also be such a sentence; otherwise, an error will be reported during running. [Html] view plaincopyprint? <Category android: name = "android. intent. category. DEFAULT"/>
<Category android: name = "android. intent. category. DEFAULT"/> use data to constrain the format.

 


Everyone should have understood the usage of action just like me!

Author: h3c4lenovo
 
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.