如何在 Webview 应用中打开深层链接

huangapple go评论55阅读模式
英文:

How to open Deep Links in webview application

问题

我已经创建了我的 WebView 应用程序,并在 onCreate 方法中加载了我的主要 URL 到 WebView 中。然后我像这样实现了深链接:

我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ariagp.myapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <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="https" android:host="mysitename.com" />
            </intent-filter>
        </activity>
    </application>

</manifest>

现在当深链接被点击时,应用程序会在 WebView 的主页上打开。但我想要打开与深链接相关的页面。

而当应用程序自身打开时,使主页打开。有什么解决方案呢?

英文:

i have created my webview application and i loaded my main url in onCreate method to webview. then i implemented the deep links like this:

My Manifest:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.ariagp.myapplication&quot;&gt;

    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;


        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;


            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
                &lt;data android:scheme=&quot;https&quot; android:host=&quot;mysitename.com&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

Now when the deep links get clicked, the app will open on the webview homepage.
But I want to make the page related to the deep link open.

And when the app itself opens, make the home page open.
what is the solution?

答案1

得分: 1

当活动被启动时,在onNewIntent(Intent intent)方法中,从intent中获取数据(它应该是点击的URL - 深链接),该数据的类型为uri,然后在您已经实现的Web视图中加载该URL。

类似以下代码:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri data = intent.getData();
        
    webView.loadUrl(data.toString());
}
英文:

When the activity is launched, in the onNewIntent(Intent intent) method, get data from intent (it is supposed to be the url clicked - deep link) which has the type uri, and then load that url in the web view that you've implemented.

Something like below code:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri data = intent.getData();
        
    webView.loadUrl(data.toString());
}

huangapple
  • 本文由 发表于 2020年10月12日 07:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定