gpt4 book ai didi

android - 如何将位于第三方应用程序或浏览器(如 whatsapp 消息)的 url 直接打开到我的 webview 应用程序中?

转载 作者:行者123 更新时间:2023-11-30 00:29:42 24 4
gpt4 key购买 nike

我有一个博客,我只使用 Webview 制作了一个应用程序版本。问题是:我想通过其他应用程序分享我的帖子,例如向某人发送 whatsapp 消息或电子邮件,其中包含指向我博客上特定帖子的链接(例如 www.blogspot.myblog.com/2017/postexample) ,然后可以选择使用我的应用程序打开它,就像 Facebook,甚至 Youtube 一样。我做了这个:

            <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="mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
<data
android:host="www.mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filter>

它确实会启动我的应用程序,但(当然)它会打开应用程序的默认页面,而不是 www.blogspot.myblog.com/2017/postexample,那么我应该怎么做才能直接打开发送的 url?谢谢大家,我们将不胜感激。

编辑

现在这是我的代码:

        <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="http"
android:host="www.blog.com"
android:pathPrefix="/" />
</intent-filter>
</activity>

和 Activity

public class BlogActivity extends AppCompatActivity {
private static final String TAG = "BlogActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();

//Get the link from that Uri
if (data != null) {
Log.d(TAG, "Data" + data.toString());

WebView WebView2 = (WebView)findViewById(R.id.web1);
WebView2.setWebViewClient(new WebViewClient());
WebView2.loadData(TAG, "text/html; charset=utf-8", "UTF-8");
WebSettings webSettings = WebView2.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}

最佳答案

看看:https://developer.android.com/training/app-indexing/deep-linking.html

来自上面的链接:

Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().

Here’s a snippet that shows how to retrieve data from an Intent:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();

//Get the link from that Uri
if(data != null) {
Log.d(TAG, "Data" + data.toString());
}
}

关于android - 如何将位于第三方应用程序或浏览器(如 whatsapp 消息)的 url 直接打开到我的 webview 应用程序中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44665334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com