Note the properties of the Intent object:

An Intent object is essentially a set of bundled information. It can be information about components that are interested in Intent, or it can be information that the Android system is interested in
. The attributes and functions of the Intent object:

Component name specifies the name of the component that processes the Intent object.
Action Intent is an action to be completed.
Category is used to describe
the type of action to be performed. Data Provides the data
to be operated on to the Action. Extras Adds attachment information to the Intent component.
Flags Instructs the Android program how to start one Activity

Component name:

Optional attribute, used to set the component name of the Inetnet object. Its attribute value is a ComponentName object. To create a ComponentName, you need to specify the package name and class name. This can uniquely determine a component class, so that the application can be based on A given component class starts a specific component.
If it is set, the Intent object will be sent to an instance of the specified class. If it is not set, Android uses other information in the Intent to locate the appropriate target component. Component name can be set by setComponent() , setClass() or setClassName() method set, and read through getComponent() method

setComponent() method:

The setComponent() method is used to set the component for the Intent, the syntax format:

public Intent setComponent(ComponentName component)

component name component to be set
when using this method, you need to create objects constructed in a manner android.content.ComponentName:
ComponentName (Context context, CLS Class <?>)
or ComponentName (String pkg, String cls)

Context is the Context (context) object, which can be specified by "current Activity name.this". The first cls is used to specify the class object of the Activity to be opened, pkg is used to specify the package name, and the second cls is used to specify to start. The complete class name (including package name)
of the Activity example:

Intent intent = new Intent();
ComponentName componentName = new ComponentName(MainActivity.this,DetailActivity.class);
intent.setComponent(componentName);

or

Intent intent = new Intent();
ComponentName componentName = new ComponentName(“com.mingrisoft”,”com.mingrisoft.DetailActivity”);
intent.setComponent(componentName);

setClass() method:

Used to set the Activity class to be opened for the Intent, the syntax format:

public Intent setClass(Context packageContext,Class<?> cls)

PackageContext Context (context) object, you can use "current Activity name.this" to specify
the class object
of the Activity to be opened by cls. Example:

Intent intent = new Intent();
			intent.setClass(MainActivity.this,DetailActivity.class);

setClassName() method:

Used to set the name of the Activity to be opened for the Intent, syntax format:

public Intent setClassName(Context packageContext,String className)

getComponent() method:

Used to get the components related to the Intent, the syntax format:

public ComponentName getComponent()

The return value is the name of the component related to the Intent, through which the package name and class name corresponding to the Intent object can be obtained.
Example:

ComponentName componentName = getIntent().getComponent();
Log.i(“MainActivity”,”包名:” + componentName.getPackageName()” +类名”+componentName.getShortClassName());

Action:

The Action attribute is used to specify the action to be performed. It largely determines how the Intent is constructed, especially the Data (data) and Extras (additional) attributes that will be introduced later. It should define the entire protocol of the Intent object that the component can handle. Instead of just defining a single Action attribute, in the Intent class, a series of action constants are defined, and the target components include Activity and Broadcast.

Standard Activity action (standard action used to start Activity):

Constant corresponding string description
1 ACTION_MAIN android.intent.action.MAIN starts as the initial Activity, no data input and output
2 ACTION_VIEW android.intent.action.VIEW displays data to the user
3 ACTION_ATTACH_DATA android.intent.action.ATTACH_DATA is used to indicate Some data should be attached to other places
4 ACTION_EDIT android.intent.action.EDIT Display the data to the user for editing
5 ACTION_PICK android.intent.action.PICK Select an item from the data and return the item
6 ACTION_CHOOSER android.intent. action.CHOOSER displays an Activity selector
7 ACTION_GET_CONTENT android.intent.action.GET_CONTENT allows the user to select a specific type of data and return it
8 ACTION_DIAL android.intent.action.DIAL Use the provided number to make a call
9.ACTION_CALL android.intent .action.CALL uses the provided data to make a call to someone
10.ACTION_SEND android.intent.action.SEND Sends a message to someone, the recipient is not specified
11.ACTION_SENDTO android.intent.action.SENDTO Sends a message to someone, receives Designated
12.ACTION_ANSWER android.intent.action.ANSWER answer the call
13.ACTION_INSERT android.intent.action.INSERT insert a blank item in the given container
14.ACTION_DELETE android.intent.action.DELETE delete the given data from the container
15.ACTION_RUN android.intent.action.RUN Unconditional run data
16.ACTION_SYNC android.intent.action.SYNC Perform data synchronization
17.ACTION_PICK_ACTIVITY android.intent.action.PICK_ACTIVITY Select the activity of the given Intent and return the selected class
18.ACTION_SEARCH android. intent.action.SEARCH execute query
19.ACTION_WEB_SEARCH android.intent.action.WEB_SEARCH execute online query
20.ACTION_FACTORY_TEST android.intent.action.FACTORY_TEST The main entry point for factory testing

In addition to custom Actions, developers can also customize Action strings to start components in the application. These newly defined strings must be prefixed with the package name of the application

The Action of an Intent object is set by the setAction() method and read by the getAction() method

setAction() method:

The setAction() method is used to set the action for the Intent, the syntax format:

public Intent setAction(String action)

action The name of the Action to be set

getAction() method:

The name of the Action used to get the Intent, syntax format:
public String getAction()

Data:

It is usually used to provide the data to be operated on to the Action. It can be a URI object, which usually includes the URI and MIME type of the data. Different Actions have different data specifications, which use the format of "data type: data"

Example:

Visit the website http: // web address
call tel: phone number
to send text messages smsto: SMS receiving number
lookup SD card file
file: /// sdcard / directory or file
Show map geo: coordinates, coordinate
contact information content: // Contact Person information

When matching an Intent to a component that can process data, it is important to clarify its data type (its MIME type) and URI. For example, a component that can display image data should not be called to play an audio file.

In most cases, the data type can be inferred from the URI, especially content: URIs, which represent the data located on the device and are controlled by the content provider, but the type can also be explicitly set, the specific method Have:

setData() method:

Used to set URI data for Intent, syntax format:

public Intent setData(Uri data)

data URI
of the data to be set Example:
Set the contact data with id 1 in the contact information:
intent.setData(Uri.parse(content://”com.android.contacts/contacts/1”));

setType() method:

The MIME type used to set the data for the Intent, the syntax format:

public Intent setType(String type)

type: The MIME type of the data to be set. For example, if you want to set the type as a photo, you can specify it as image/ . If you want to set the type as a video, you can specify it as video /
Example:

intent.setType(“image/*”);

setDataAndType()方法:

Used to set the data and its MIME type for the Intent, syntax format:

public Intent setDataAndType(Uri data,String type)

data: URI of the data to be set
type: MIME type of the data to be set

Example: The
specified data is the img01.png picture in the pictures directory on the SDCard

Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath() + “/pictures/img01.png”));
intent.setDataAndType(uri,”image/*”);

putExtra(String,String) method:

Set the message content when sending the message
String1: Set the type of message to be sent
String2: Set the message content to be sent

Two methods commonly used when reading data:

getData() method:

Used to obtain data related to Intent, syntax format:

public Uri getData()

Example:
To get the URI of the current intent related data

Uri uri = intent.getData();

getType() method:

The MIME type used to obtain the data related to the Intent, the syntax format:

public String getType()

Example:
To get the data type of the current intent object

String type = intent.getType();

Category:

**这个属性用来对执行动作的类别进行描述,开发人员可以在一个Intent对象中指定任意数量的Category**

addCategory() method:

	**用来为Intent添加种类信息,语法格式:**

public Intent addCategory(String category)
Example:
To set Category as the system desktop:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(“android.intent.category.HOME”);

removeCategory() method:

Used to delete the specified type of information from the Intent, the syntax format:

public void removeCategory(String category)

Example:
To remove the type information set as the system desktop:

intent.removeCategory(“android.intent.category.HOME”);

getCategories() method:

Used to obtain all kinds of information related to Intent, syntax format:

public Set<String> getCategories()
Example:
To obtain all types of information related to Intent

Set<String> set = intent.getCategories();

Extras (additional information):

Used to add additional information to the Intent component, usually in the form of key-value pairs to save additional information

putExtras() method:

Used to add additional information to the Intent, syntax format:

public Intent putExtras(Bundle extras)

extras: Bundle object that saves additional information
Example:
Create a Bundle object, save name information, and add it to the Intent object through the putExtras() method:

Bundle bundle = new Bundle();
bundle.putCharSequence(“name”,”mr”); //保存名称信息
intent.putExtras(bundle);

getExtras() method:

Used to obtain additional information in the Intent, the syntax format:

public Bundle getExtras()

The utExtras() method and getExtras() method are usually used to pass values ​​between multiple Activities

Flags:

Mainly used to instruct the Android program how to start an Activity
Task is a collection of Activity components gathered together in a stack mode. The
default system does not include graphical task management functions. Try not to use the FLAG_ACTIVITY_MULTIPLE_TASK logo unless you can provide the user with a way -You can return to the started Task

setFlags () Method:

Used to set the flag for the Intent, multiple use will replace the previous one, the syntax format:

public Intent setFlags(int flags)

Example:
To set a new activity not to be retained in the history stack, once the user leaves it, the activity is automatically closed:

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

addFlags() method:

Used to add flags to the Intent, multiple use will add multiple flags, syntax format:

public Intent addFlags(int flags)

Example:
To set up Activity to start in a new Task:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

getFlags () Method:

Used to get the Intent flag, syntax format:

public int getFlags()

Example:
To get the logo saved in the Intent and output it in the LogCat panel:

Log.i(“标志”,String.valueOf(intent.getFlags()));

Guess you like

Origin blog.csdn.net/qq_42823109/article/details/96857082