本文介绍了如何让我的网络视图加载与其他应用共享的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不熟悉开发,对于我的第一个应用程序,我正尝试制作一个网络浏览器.我的问题是我无法从共享的Intent获取Web视图来加载URL.我所得到的只是一个空白页.我花了几天时间在网上搜索,任何帮助将不胜感激.预先谢谢你!
I'm new to developing, for my first app I'm trying to make a web browser. My problem is I cant get my webview to load a url from a shared Intent. All I get is a blank page. I've spent days searching the web, any help would be greatly appreciated. Thank you in advance!
我的代码(在oncreate和onresume中)获得意图
my code to get the intent (in oncreate and onresume)
Intent receivedIntent = getIntent();
String receivedAction = receivedIntent.getAction();
String receivedType = receivedIntent.getType();
try {
if (receivedAction.equals(Intent.ACTION_VIEW)) {
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);
if (receivedText != null) {
if (receivedType.equals("text/plain")) {
webView.loadUrl(receivedText);
}
}
我的清单
<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.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
推荐答案
您可以执行以下操作
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="org.sakaiproject.sakai.WebViewActivity" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/webview_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_alignParentTop="true"
android:focusable="false" />
</RelativeLayout>
现在,在 Activity
上,您必须使用 getDataString()
Now on the Activity
you must get the url with getDataString()
public class WebViewActivity extends AppCompatActivity {
private WebView webView;
private ProgressBar progressBar;
private String url = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
// get the url
url = getIntent().getDataString();
progressBar = (ProgressBar) findViewById(R.id.webview_progressbar);
webView = (WebView) findViewById(R.id.webview);
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int progress) {
// update progressbar
progressBar.setProgress(progress);
}
});
webView.setWebViewClient(new WebClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
}
@Override
public void onBackPressed() {
if (webView.canGoBack())
webView.goBack();
else
super.onBackPressed();
}
public class WebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// return super.shouldOverrideUrlLoading(view, url);
progressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return false;
}
}
}
和清单上
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
这篇关于如何让我的网络视图加载与其他应用共享的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!