Intent-related content adjustment, intent-related content

Source: Internet
Author: User

Intent-related content adjustment, intent-related content

1. inten attributes
ComponentName, Action, Category, Data, Type, Extra, and Flag;
Intent intent = new Intent ();

1, ComponentName intent. setComponent (new ComponentName ("com. example. tahlia. intent_exercise_01 "," com. example. tahlia. intent_exercise_01.Aty1 "); input parameters for ComponentName instances: (String pkg, String cls), (Context pkg, String cls), (Context pkg, Class
  Cls), (Parcel in) Note: (1) There are two situations when starting activity in different apps. The first is that the activity has a filter.
  
   
Configuration. The system has the android: exported = "true" attribute by default. Otherwise, this attribute is added during startup. Otherwise, you are not authorized to start it! (2) parameter issues in ComponentName. The two parameters are ("The package to be started" and "the package to be started" respectively. class Name "); 2. Application of Action and Category; (1) first, Action indicates the Activity to be started, and the configuration in the Activity configuration to be started is as follows:
  

 

The following statement can be called to start it:

Define static constants in the Aty1 class:
Public static final String ACTION_START = "comd. example. user. Specify intent. intent. action. Aty1 ";

@Override public void onClick(View view) {
Intent intent; switch (view.getId()){ case R.id.BtnStartAty1_01:
intent = new Intent(); intent.setAction(“abcdefghijklmnopqrstuvwxyz”); startActivity(intent); break; case R.id.BtnStartAty1_02:
intent = new Intent(“comd.example.user.baiintent.intent.action.Aty1”); startActivity(intent); break; case R.id.BtnStartAty1_03:
intent = new Intent(Aty1.ACTION_START); startActivity(intent); break;
case R.id.BtnStartAty1_04:
intent = new Intent(“test”); startActivity(intent); break; }
}

When Intent is used for implicit start, you can set the action through setAction, or pass in the action through the Intent constructor. The two statements have the same effect.
Action matching rules

Action is a user-defined string used to describe the component of the current defined action. I think it is a name of Activity. There are two matching rules for action:

1. For an Activity with an action set, if this Activity is started implicitly, its action must be matched.

2. Multiple actions can be set for an Activity. You only need to match one of the actions when matching.

This rule can be understood as follows: there is a person named Wang Wu. Everyone called him Lao Wang, and he was also named Wang fatzi. If you want to say hello to him, "Wang Wu", "Lao Wang", and "Wang fatzi", you can all get his response. However, if he is called Lao Zhang, he will not give any response because he is not Lao Zhang.

Because the action is started implicitly, add the action attribute value to Aty1 first. Action is actually a user-defined case-sensitive string, so any string can be used. However, to better classify different classes, the general syntax is "package name + intent. action. Class Name ". Sometimes this write method makes the value of action longer. To reduce errors, a static constant is declared in the started class to store the value of action.

Note:

Once used  The label must be defined.  Otherwise, the program reports an error. Because we do not use Intent. addCategory to add category when calling Activity through intent, Android will automatically add "android. intent. category. DEFAULT" by DEFAULT.  In the configuration, category_DEFAULT is indispensable.   

In addition, when we configure the action, the system also provides common property value prompts, the following example:

ACTION_MAIN

Android Application portal. Each APP must have only one Activity containing one Action Declaration of this type.

ACTION_DIAL

Open the default dialing program. If a phone number is set in Data, the system automatically enters the phone number in the dialing program.

ACTION_CALL

Directly call the phone number set for Data.

ACTION_VIEW

Display different Data types

ACTION_SEND

The User specifies the sending method to send data

(2) Category matching rules
The matching rules of Category are actually in the case of several activities, but their actions are the same, but they have different category values. When intent is used, intent is added. addCategory (""); To match whether the activity contains the declaration of this category; as long as it contains several items, it will become one of the list to be selected ;~ Combined Use of Action and category;
※Note:
Category_DEFAULT is an attribute value that is essential for implicitly starting an Activity. By default, Android adds intent to the intent that is implicitly started. addCategory ("android. intent. category. "DEFAULT"); this statement indicates that Android will treat it as an Activity for processing and execution. Therefore, we should add category_DEFAULT to the Activity. (Except for portal activities of the app, you can also add them manually without adding them)

In addition, when we configure category, the system also provides common property value prompts, the following example:

CATEGORY_DEFAULT

The default execution method in Android system is executed according to the normal Activity execution method.

CATEGORY_HOME

Set this component to Home Activity.

CATEGORY_LAUNCHER

Set this component to the Activity with the highest priority in the current application initiator. It is usually used with the entry ACTION_MAIN.

CATEGORY_BROWSABLE

You can set this component to start in a browser.
Let the application return to the Home Desktop

 Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent);

(3) Data and Type attributes
These two attributes also match the activity started by intent, but the intent parameter is a uri
The Uri format is as follows: scheme: // host: port/path
The startup code is as follows:

Public void scheme (View source)
{
Intent intent = new Intent ();
// Only set the Data attribute of Intent
Intent. setData (Uri. parse ("lee: // www.crazyit.org: 1234/test "));
StartActivity (intent );
}
Public void schemeHostPort (View source)
{
Intent intent = new Intent ();
// Only set the Data attribute of Intent
Intent. setData (Uri. parse ("lee: // www.fkjava.org: 8888/test "));
StartActivity (intent );
} Public void schemeHostPath (View source)
{
Intent intent = new Intent ();
// Only set the Data attribute of Intent
Intent. setData (Uri. parse ("lee: // www.fkjava.org: 1234/mypath "));
StartActivity (intent );
}
Public void schemeHostPortPath (View source)
{
Intent intent = new Intent ();
// Only set the Data attribute of Intent
Intent. setData (Uri. parse ("lee: // www.fkjava.org: 8888/mypath "));
StartActivity (intent );
}
Public void schemeHostPortPathType (View source)
{
Intent intent = new Intent ();
// Set the Data and Type attributes of Intent at the same time
Intent. setDataAndType (Uri. parse ("lee: // www.fkjava.org: 8888/mypath"), "abc/xyz ");
StartActivity (intent );
}

In this way, the matching activity is started, as long as all data and Type configurations in an activity configuration correspond to the Uri above (matched ).
String, then the activity will be included in the list to be started, and only one will start it directly;

Data and Type have the following features:

Public void overrideType (View source ){
Intent intent = new Intent ();
// Set the Type attribute for intent first
Intent. setType ("abc/xyz ");
// Set the data attribute for intent to overwrite the Type attribute.
Intent. setData (Uri. parse ("lee: // www.fkjava.org: 8888/test "));
Toast. makeText (this, intent. toString (), Toast. LENGTH_LONG). show ();
}
Public void overrideData (View source ){
Intent intent = new Intent ();
// Set the Data attribute for intent first
Intent. setData (Uri. parse ("lee: // www.fkjava.org: 8888/mypath "));
// Set the Type attribute for intent to overwrite the data attribute.
Intent. setType ("abc/xyz ");
Toast. makeText (this, intent. toString (), Toast. LENGTH_LONG). show ();
}
Public void dataAndType (View source ){
Intent intent = new Intent ();
// Set the data and Type attributes of intent at the same time
Intent. setDataAndType (Uri. parse ("lee: // www.fkjava.org: 8888/mypath"), "abc/xyz ");
Toast. makeText (this, intent. toString (), Toast. LENGTH_LONG). show ();
}

(4) use of Extra
First, it is clear that the Bundle object is actually uploaded with intent, and the Bundle object can carry data in the form of put;
Therefore, it is essentially a Bundle value;
The process of passing values is as follows:

Intent intent = new Intent (getApplicationContext (), SecondActivity. class); // The first method directly uses intent. putExtra encapsulation, which is essentially a Bundle) intent. putExtra ("id", id); intent. putExtra ("name", name); intent. putExtra ("salary", salary);/** the second method uses Bundle to encapsulate data */Bundle bundle = new Bundle (); bundle. putInt ("id", id); bundle. putString ("name", name); bundle. putFloat ("salary", salary); intent. putExtras (bundle); Value process: Intent intent = getIntent ();/* Note that the imported Intent object is obtained using getIntent, when activating an Activty component, Intent will also pass the intent object */int id = intent. getIntExtra ("id",-1); String name = intent. getStringExtra ("name"); float salary = intent. getFloatExtra ("salary",-1f );

(5) Flag
The following are several forms of startup;
(Similar to the activity Loading Mode !)

FLAG_ACTIVITY_BROUGHT_TO_FRONTFLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_NO_ANIMATIONFLAG_ACTIVITY_NO_HISTORYFLAG_ACTIVITY_REORDER_TO_FRONTFLAG_ACTIVITY_SINGLE_TOP

Related Article

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.