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

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

How to open Deep Links in webview application

问题

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

我的清单文件:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.ariagp.myapplication">
  3. <uses-permission android:name="android.permission.INTERNET" />
  4. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  6. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/AppTheme">
  14. <activity android:name=".MainActivity">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. <intent-filter>
  20. <action android:name="android.intent.action.VIEW" />
  21. <category android:name="android.intent.category.DEFAULT" />
  22. <category android:name="android.intent.category.BROWSABLE" />
  23. <data android:scheme="https" android:host="mysitename.com" />
  24. </intent-filter>
  25. </activity>
  26. </application>
  27. </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:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. package=&quot;com.ariagp.myapplication&quot;&gt;
  4. &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
  5. &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
  6. &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
  7. &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
  8. &lt;application
  9. android:allowBackup=&quot;true&quot;
  10. android:icon=&quot;@mipmap/ic_launcher&quot;
  11. android:label=&quot;@string/app_name&quot;
  12. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  13. android:supportsRtl=&quot;true&quot;
  14. android:theme=&quot;@style/AppTheme&quot;&gt;
  15. &lt;activity android:name=&quot;.MainActivity&quot;&gt;
  16. &lt;intent-filter&gt;
  17. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  18. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  19. &lt;/intent-filter&gt;
  20. &lt;intent-filter&gt;
  21. &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
  22. &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
  23. &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
  24. &lt;data android:scheme=&quot;https&quot; android:host=&quot;mysitename.com&quot; /&gt;
  25. &lt;/intent-filter&gt;
  26. &lt;/activity&gt;
  27. &lt;/application&gt;
  28. &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。

类似以下代码:

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

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:

  1. @Override
  2. protected void onNewIntent(Intent intent) {
  3. super.onNewIntent(intent);
  4. Uri data = intent.getData();
  5. webView.loadUrl(data.toString());
  6. }

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:

确定