Intent屬性詳解三 data、type和extra

来源:http://www.cnblogs.com/androidWuYou/archive/2016/09/26/5888280.html
-Advertisement-
Play Games

1 Data 執行時要操作的數據 在目標<data/>標簽中包含了以下幾種子元素,他們定義了url的匹配規則: android:scheme 匹配url中的首碼,除了“http”、“https”、“tel”...之外,我們可以定義自己的首碼 android:host 匹配url中的主機名部分,如“g ...


1 Data  執行時要操作的數據

在目標<data/>標簽中包含了以下幾種子元素,他們定義了url的匹配規則:

android:scheme 匹配url中的首碼,除了“http”、“https”、“tel”...之外,我們可以定義自己的首碼

android:host 匹配url中的主機名部分,如“google.com”,如果定義為“*”則表示任意主機名

android:port 匹配url中的埠

android:path 匹配url中的路徑

在XML中聲明可以操作的data域應該是這樣的:

<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>  

 

註意:

這個標識比較特殊,它定義了執行此Activity時所需要的數據,也就是說,這些數據是必須的!!!!!所有如果其它條件都足以激活該Activity,但intent卻沒有傳進來指定類型的Data時,就不能激活該activity!!!!

2 Intent的Type屬性

Intent的Type屬性顯式指定Intent的數據類型(MIME)。一般Intent的數據類型能夠根據數據本身進行判定,但是通過設置這個屬性,可以強制採用顯式指定的類型而不再進行推導。

3 方法

1  settype

使用該函數表示要查找文件的mime類型(如*/*),這個和組件在manifest里定義的相對應,但在源代碼里:

public Intent setData(Uri data) { 

        mData = data; 

        mType = null; 

        return this; 

    } 

 

會將type設為null。

2  setdata

該函數的參數是uri,所以要將數據通過該函數傳遞時,記得要把數據轉化為uri,如Uri.fromFile(new File("/mnt/sdcard/"))。

該函數源代碼

public Intent setType(String type) { 

        mData = null; 

        mType = type; 

        return this; 

    } 

 

3 setdataandtype

所以要同時設置data和type的話只能用函數setdataandtype了

public Intent setDataAndType(Uri data, String type) { 

        mData = data; 

        mType = type; 

        return this; 

    } 

4 Extras:

  Extras屬性主要用於傳遞目標組件所需要的額外的數據。通過putExtras()方法設置。

    常用值如下所示:

    EXTRA_BCC:存放郵件密送人地址的字元串數組。 

    EXTRA_CC:存放郵件抄送人地址的字元串數組。

    EXTRA_EMAIL:存放郵件地址的字元串數組。 

    EXTRA_SUBJECT:存放郵件主題字元串。 

    EXTRA_TEXT:存放郵件內容。 

    EXTRA_KEY_EVENT:以KeyEvent對象方式存放觸發Intent的按鍵。  

    EXTRA_PHONE_NUMBER:存放調用ACTION_CALL時的電話號碼

 

5 Demo源碼

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);
    }

}

 清單文件

<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="含有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="含有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="含有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="含有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和type同時存在">
            <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>

 

本人微博:honey_11

Demo下載
最後,以上例子都來源與安卓無憂,請去應用寶或者豌豆莢下載:例子源碼,源碼例子文檔一網打盡

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • js中三種彈窗 1)alert 彈出警告 無返回值---------alert('第一行\n第二行'); 2)confirm()選擇確定或取消,返回t或f----var result = confirm('是否刪除!'); 3)prompt()彈出輸入框,返回輸入內容----var value = ...
  • 以下是個人對這三個老大難的總結(最近一直在學習原生JS,翻了不少書,不少文檔,雖然還是新手,但我會繼續堅持走我自己的路) 原型鏈 所有對象都是基於 ,`Object.prototype Object.prototype Object.prototype toString() [[class]] '[ ...
  • <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1, ...
  • 本文是翻譯Function.apply and Function.call in JavaScript,希望對大家有所幫助 轉自“http://www.jb51.net/article/52416.htm” 第一次翻譯技術文章,見笑了! 翻譯原文: Function.apply and Functi ...
  • 1.定義網頁背景顏色 <body bgcolor="背景色"> 顏色可以用2種方式表示:1. 直接指定顏色名稱,如blue。2.使用十六進位數據表示如#RRGGBB,分別表示兩位十六進位數據. 2.設置背景圖片 <body background="圖片的地址"> 3.設置文字顏色 <body tex ...
  • 正常情況下 使用bootstrap 比原生代碼 寫響應式佈局更快 補充上一文《bootstrap的實際應用》: web端 必做相容性 適應各種瀏覽器 可能有幾套圖 經常使用雪碧圖 app端 有不同的屏幕尺寸 且必須進行自適應{ @media srceen 或者 bootstrap 或者 JS } 也 ...
  • 仿Bilibili iOS客戶端 練習啟動頁 源碼下載:http://code.662p.com/view/14534.html 首頁 分區 發現 我的 視頻信息 普通/直播 視頻播放 詳細說明:http://ios.662p.com/thread-3121-1-1.html ...
  • 禮物說仿寫(updating...) 源碼下載:http://code.662p.com/view/14507.html api: 禮物說 首頁精選 banner2: http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=2 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...