如何使用自定义URI方案打开应用程序?

时间:2015-08-09 10:07:37

标签: android custom-url

我在询问之前查看了不同的帖子,但没有一个能解决我的问题。

我想用自定义URI方案打开应用程序:myapp://

我尝试了intent.setData(Uri.parse("myapp://")),但它无效。

所以我错过了什么?

2 个答案:

答案 0 :(得分:0)

试试这个:

intent.setData(Uri.parse("myapp://path"))

<activity android:name=".YourActivity">
    <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:scheme="myapp" android:host="path" />
    </intent-filter>
</activity>

答案 1 :(得分:0)

首先,您应该在AndroidManifest.xml中定义正确的intent-filter

<application
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="browse"
                    android:pathPrefix=""
                    android:scheme="myapp" />
            </intent-filter>
        </activity>
</application>

然后在你的Java代码中处理这个意图:

if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
    List<String> segments = getIntent().getData().getPathSegments();
    String section = getIntent().getData().getHost();
    if (segments.size() > 1 && segments.get(0).equals("browse")) {
        // Do something here
    }
}
相关问题