android 定义 程序 Scheme 接收特定URI开启Activity

时间:2021-06-17 20:40:38

场景:通过浏览器打开URL或者扫描软件扫描URL来启动本地应用

<intent-filter>
<category android:name="android.intent.category.DEFAULT"></category>
<action android:name="android.intent.action.VIEW"></action>
<data android:scheme="sh"></data>
</intent-filter>

这样即指定了接收Uri的Scheme为sh 且 Action为View的Intent。

利用如下Intent调用Activity

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sh://123123123")));

在接收的Activity中使用如下代码获得数据

this.getIntent().getScheme();//获得Scheme名称
this.getIntent().getDataString();//获得Uri全部路径