簡(jiǎn)介
通用資源標(biāo)志符(Universal Resource Identifier, 簡(jiǎn)稱(chēng)"URI")。
Uri代表要操作的數(shù)據(jù),Android上可用的每種資源 - 圖像、視頻片段等都可以用Uri來(lái)表示。從概念上來(lái)講,URI包括URL。
URI命名規(guī)則
URI一般由三部分組成:
在Android平臺(tái),URI主要分三個(gè)部分:scheme, authority and path。
其中authority又分為host和port。格式如下:scheme://host:port/path
例如:

一些常用URI
- 打開(kāi)一個(gè)網(wǎng)頁(yè),類(lèi)別是Intent.ACTION_VIEW
Uri uri = Uri.parse(
"[http://www.baidu.com/](http://www.baidu.com/)"
);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- 打開(kāi)地圖并定位到一個(gè)點(diǎn)
Uri uri = Uri.parse("geo:52.76,-79.0342");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- 打開(kāi)撥號(hào)界面,類(lèi)型是Intent.ACTION_DIAL
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
- 直接撥打電話,與之不同的是,這個(gè)直接撥打電話,而不是打開(kāi)撥號(hào)界面
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
- 卸載一個(gè)應(yīng)用,Intent的類(lèi)別是Intent.ACTION_DELETE
Uri uri = Uri.fromParts("package", "xxx", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
更多請(qǐng)參考: android 常用URI 值得記住
通過(guò)URI跳轉(zhuǎn)到Activity
實(shí)現(xiàn)從H5或第三方應(yīng)用點(diǎn)擊進(jìn)入某應(yīng)用。
首先,在AndroidManifest.xml里面進(jìn)行配置,在對(duì)應(yīng)的Activity加上一個(gè)intent-filter, 如下:
<activity
android:name=".DestActivity"
android:configChanges="orientation|keyboardHidden"
android:exported="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="www.marsthink.com"
android:path="/travel/oversea/"
android:scheme="imarsthink" />
</intent-filter>
</activity>
對(duì)應(yīng)的Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri uri = intent.getData();
if (uri != null) {
String host = uri.getHost();
String dataString = intent.getDataString();
String id = uri.getQueryParameter("id");
String path = uri.getPath();
String path1 = uri.getEncodedPath();
String queryString = uri.getQuery();
Log.d("Alex", "host:"+host);
Log.d("Alex", "dataString:" + dataString);
Log.d("Alex", "id:" + id);
Log.d("Alex", "path:" + path);
Log.d("Alex", "path1:" + path1);
Log.d("Alex", "queryString:" + queryString);
}
}
}
被調(diào)起時(shí)的log日志:
host:www.marsthink.com
dataString:imarsthink://www.marsthink.com/travel/oversea?id=1000
id:1000
path:/travel/oversea
path1:/travel/oversea
queryString:id=1000
第三方App調(diào)起方式:
Uri uri = Uri.parse("imarsthink://www.marsthink.com/travel/oversea?id=1000");
Intent intent = new Intent(null, uri);
startActivity(intent);
H5調(diào)起方式:
<a href="imarsthink://www.marsthink.com/travel/oversea?id=1000">open Android app</a>
UriMatcher
UriMatcher 類(lèi)主要用于匹配Uri.
使用方法如下,
首先第一步,初始化:
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
第二步注冊(cè)需要的Uri:
matcher.addURI("com.yfz.Lesson", "people", PEOPLE);
matcher.addURI("com.yfz.Lesson", "person/#", PEOPLE_ID);
第三部,與已經(jīng)注冊(cè)的Uri進(jìn)行匹配:
Uri uri = Uri.parse("content://" + "com.yfz.Lesson" + "/people");
int match = matcher.match(uri);
switch (match)
{
case PEOPLE:
return "vnd.Android.cursor.dir/people";
case PEOPLE_ID:
return "vnd.android.cursor.item/people";
default:
return null;
}
match方法匹配后會(huì)返回一個(gè)匹配碼Code,即在使用注冊(cè)方法addURI時(shí)傳入的第三個(gè)參數(shù)。
上述方法會(huì)返回
vnd.Android.cursor.dir/person
ContentUris
ContentUris 類(lèi)用于獲取Uri路徑后面的ID部分
1)為路徑加上ID: withAppendedId(uri, id)
比如有這樣一個(gè)Uri
Uri uri = Uri.parse("content://com.yfz.Lesson/people")
通過(guò)withAppendedId方法,為該Uri加上ID
Uri resultUri = ContentUris.withAppendedId(uri, 10);
最后resultUri為: content://com.yfz.Lesson/people/10
2)從路徑中獲取ID: parseId(uri)
Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")
long personid = ContentUris.parseId(uri);
最后personid 為 :10