Description of Intent attributes: data, type, extra, and intentextra

Source: Internet
Author: User

Description of Intent attributes: data, type, extra, and intentextra

1. Data to be operated during Data Execution

The target <data/> tag contains the following seed elements, which define url matching rules:

Android: scheme matches the prefix in the url. Apart from "http", "https", and "tel"..., we can define our own prefix.

Android: host matches the host name in the url, for example, google.com. If it is defined as "*", it indicates any host name.

Android: port matches the port in the url

Android: path matches the path in the url

Declare the data fields that can be operated in XML as follows:

<activity android:name=".TargetActivity">  <intent-filter>      <action android:name="com.scott.intent.action.TARGET"/>      <category android:name="android.intent.category.DEFAULT"/>      <data android:scheme="scott" android:host="com.scott.intent.data" android:port="7788" android:path="/target"/>  </intent-filter>  </activity>  

 

Note:

This identifier is special. It defines the data required to execute this Activity. That is to say, the data is required !!!!! If all other conditions are sufficient to activate the Activity, but the intent has not passed in the specified type of Data, the activity cannot be activated !!!!

2. Type attribute of Intent

The Type attribute of Intent explicitly specifies the data Type (MIME) of Intent ). Generally, the Intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.

3 method 1 settype

Use this function to indicate the mime type (such as */*) of the file to be searched. This corresponds to the definition of the component in manifest, but in the source code:

public Intent setData(Uri data) {         mData = data;         mType = null;         return this;     } 

 

Set type to null.

2 setdata

The parameter of this function is uri, so when you want to pass data through this function, remember to convert the data into a uri, such as Uri. fromFile (new File ("/mnt/sdcard /")).

Source code of the Function

public Intent setType(String type) {         mData = null;         mType = type;         return this;     } 

 

3 setdataandtype

So if you want to set both data and type, you can only use the setdataandtype function.

Public Intent setDataAndType (Uri data, String type ){

MData = data;

MType = type;

Return this;

}

4 Extras:

The Extras attribute is used to transmit the additional data required by the target component. Use the putExtras () method.

Common values are as follows:

EXTRA_BCC: A String Array that stores the BCC address.

EXTRA_CC: A String Array that stores the CC address.

EXTRA_EMAIL: an array of strings that store mail addresses.

EXTRA_SUBJECT: stores the subject string of the email.

EXTRA_TEXT: stores the mail content.

EXTRA_KEY_EVENT: stores the keys that trigger Intent as KeyEvent objects.

EXTRA_PHONE_NUMBER: The phone number used to call ACTION_CALL.

 

5 Demo source code

Activity:

package mm.shandong.com.testdatatype;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.TextView;import java.util.ArrayList;public class TestDataTypeActivity extends AppCompatActivity {    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_data_type);        textView= (TextView) findViewById(R.id.textView);    }    public void readDataAndType1(View view){            Intent intent=new Intent();           Uri uri= Uri.parse("http://www.baidu.com/2.asp");           intent.setData(uri);           intent.setType("abc/efg");           String str="Data: "+intent.getDataString()+", Type:"+intent.getType();          textView.setText(str);    }    public void readDataAndType2(View view){        Intent intent=new Intent();        intent.setType("abc/efg");        Uri uri= Uri.parse("http://www.baidu.com/2.asp");        intent.setData(uri);        String str="Data: "+intent.getDataString()+", Type:"+intent.getType();        textView.setText(str);    }    public void readDataAndType3(View view){        Intent intent=new Intent();        Uri uri= Uri.parse("http://www.baidu.com/2.asp");        intent.setDataAndType(uri,"abc/efg");        String str="Data: "+intent.getDataString()+", Type:"+intent.getType();        textView.setText(str);    }    public void startDataAndType1(View view){        Intent intent=new Intent();        intent.setAction("TestDataTypeActivityXXX");        Uri uri= Uri.parse("ottp://");        intent.setData(uri);        startActivity(intent);    }    public void startDataAndType2(View view){        Intent intent=new Intent();        intent.setAction("TestDataTypeActivityXXX");        Uri uri= Uri.parse("ottp://shandong.mm");        intent.setData(uri);        startActivity(intent);    }    public void startDataAndType3(View view){        Intent intent=new Intent();        intent.setAction("TestDataTypeActivityXXX");        Uri uri= Uri.parse("ottp://shandong.mm:8080");        intent.setData(uri);        startActivity(intent);    }    public void startDataAndType4(View view){        Intent intent=new Intent();        intent.setAction("TestDataTypeActivityXXX");        Uri uri= Uri.parse("ottp://shandong.mm:8080/pathparent/pathchild");        intent.setData(uri);        startActivity(intent);    }    public void startDataAndType5(View view){        Intent intent=new Intent();        intent.setAction("TestDataTypeActivityXXX");        Uri uri= Uri.parse("ottp://shandong.mm:8080/pathparent/pathchild");        intent.setDataAndType(uri,"abc/efg");        startActivity(intent);    }}

Configuration File

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "mm.shandong.com. testdatatype "> <application android: allowBackup =" true "android: icon =" @ mipmap/ic_launcher "android: label =" @ string/app_name "android: supportsRtl = "true" android: theme = "@ style/AppTheme"> <activity android: name = ". testDataTypeActivity "android: configChanges =" keyboardHidden | orientation | screenSize "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <activity android: name = ". testDataTypeActivity1 "android: configChanges =" keyboardHidden | orientation | screenSize "android: label =" contains scheme "> <intent-filter> <action android: name = "TestDataTypeActivityXXX"/> <category android: name = "android. intent. category. DEFAULT "/> <data android: scheme =" ottp "/> </intent-filter> </activity> <activity android: name = ". testDataTypeActivity2 "android: configChanges =" keyboardHidden | orientation | screenSize "android: label =" contains host "> <intent-filter> <action android: name = "TestDataTypeActivityXXX"/> <category android: name = "android. intent. category. DEFAULT "/> <data android: host =" shandong. mm "android: scheme =" ottp "/> </intent-filter> </activity> <activity android: name = ". testDataTypeActivity3 "android: configChanges =" keyboardHidden | orientation | screenSize "android: label =" include port "> <intent-filter> <action android: name = "TestDataTypeActivityXXX"/> <category android: name = "android. intent. category. DEFAULT "/> <data android: host =" shandong. mm "android: port =" 8080 "android: scheme =" ottp "/> </intent-filter> </activity> <activity android: name = ". testDataTypeActivity4 "android: configChanges =" keyboardHidden | orientation | screenSize "android: label =" contains path "> <intent-filter> <action android: name = "TestDataTypeActivityXXX"/> <category android: name = "android. intent. category. DEFAULT "/> <data android: host =" shandong. mm "android: path ="/pathparent/pathchild "android: port =" 8080 "android: scheme = "ottp"/> </intent-filter> </activity> <activity android: name = ". testDataTypeActivity5 "android: configChanges =" keyboardHidden | orientation | screenSize "android: label =" data and type coexist "> <intent-filter> <action android: name = "TestDataTypeActivityXXX"/> <category android: name = "android. intent. category. DEFAULT "/> <data android: host =" shandong. mm "android: mimeType =" abc/efg "android: path ="/pathparent/pathchild "android: port =" 8080 "android: scheme = "ottp"/> </intent-filter> </activity> </application> </manifest>

 

My microblog: honey_11

Download Demo
Finally, the above examples all come from Android. Please download the sample source code from app Bao or pods: the source code is exhausted by the source code.

 

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.