android scheme

有時(shí)候我們開(kāi)發(fā)的應(yīng)用希望讓其他應(yīng)用也可以訪問(wèn),Android平臺(tái)而言,可以通過(guò)Uri(統(tǒng)一資源標(biāo)識(shí)符Uniform Resource Identifier)來(lái)實(shí)現(xiàn).

Android中 URI的組成部分scheme, authority and path,其中authority又分為host和port。格式如下: scheme://host:port/path
舉個(gè)實(shí)際的例子:
content://com.dx.test:2020/folder/myfolder/dir
其中scheme 對(duì)應(yīng) content://
authority 對(duì)應(yīng) com.dx.test:2020
host 對(duì)應(yīng) com.dx.test
port 對(duì)應(yīng) 2020
path 對(duì)應(yīng) /folder/myfolder/dir
這時(shí)候我們想到在mainifest.xml里面定義的activity標(biāo)簽下的intent-filter子標(biāo)簽data里面的各個(gè)屬性,實(shí)際上與上面是有對(duì)應(yīng)關(guān)系的

<data android:host="string"    
   android:mimeType="string"    
   android:path="string"    
   android:pathPattern="string"  
   android:pathPrefix="string"
   android:port="string" 
   android:scheme="string" /> 

比如有在A應(yīng)用程序的mainifest.xml中有這么個(gè)Activity

     <activity android:name=".TestActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sharetest" android:host="data" />
            </intent-filter>
        </activity>

如上所示,在data里設(shè)置了 scheme和host,則該Activity可以接收和處理類似于 "sharetest://data/XXX"的鏈接。
在A應(yīng)用程序的TestActivity中編寫(xiě)處理Scheme的邏輯

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by dengxuandeng on 16-3-9.
 */
public class TestActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        Intent a = getIntent();
        Log.d("scheme", a.toString());
    }
}

這里為了方便 直接用log打出來(lái)了.
到這里 A 程序就準(zhǔn)備OK了.

在B應(yīng)用程序中,可以直接寫(xiě)一個(gè)函數(shù) 調(diào)起A引用程序中的這個(gè)TestActivity

public void gotoScheme(String url) {
        Intent intent = new Intent(Intent.ACTION_DEFAULT, Uri.parse(url));
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        this.startActivity(intent);
    }

調(diào)起的時(shí)候直接寫(xiě)

gotoScheme("sharetest://data/123141")

那么使用B程序使用Scheme調(diào)起 A程序的TestActivity的時(shí)候,可以看到A程序的TestActivity打出來(lái)的log

03-09 11:35:48.126 1088-1088/com.dear.schemetest D/scheme: Intent { act=android.intent.action.VIEW dat=sharetest://data/123141 flg=0x24000000 cmp=com.dear.schemetest/.TestActivity (has extras) }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 大家都知道,如果我們想要打開(kāi)手機(jī)本地的其他應(yīng)用,我們可以通過(guò)intent的隱式啟動(dòng),添加相關(guān)界面activity的...
    Jafir閱讀 27,935評(píng)論 8 50
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評(píng)論 25 709
  • Uri的格式:scheme://host:port/path or pathPrefix or pathPatte...
    柒黍閱讀 6,295評(píng)論 0 3
  • 一、我俗?請(qǐng)問(wèn):誰(shuí)不俗? 最近,我換了工作,從體制里出來(lái),就好像每天的頭條新聞?lì)A(yù)測(cè)的一般,傳統(tǒng)媒體人殺出圍城,大踏...
    左十Hani閱讀 456評(píng)論 0 1

友情鏈接更多精彩內(nèi)容