【问题标题】:Create simple WebView with action.VIEW使用 action.VIEW 创建简单的 WebView
【发布时间】:2019-10-09 23:05:13
【问题描述】:

您好,我创建了一个简单的链接浏览器,但我丢失了上次打开外部 URL 的更新。

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.browser">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.test.browser.WebViewActivity"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <intent-filter>
                <data android:scheme="http" />
                <data android:scheme="https" />
                <action android:name="android.intent.action.PICK" />
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这是我的简单WebViewActivity.java

public class WebViewActivity extends AppCompatActivity {
    public static Activity controller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        controller = this;
        mURL = "";
        if (getIntent().getData() != null) {
            setWebViewAndLoadURL(mURL);
        } else {
            this.finishAffinity();
        }
    }
}

“setWebViewAndLoadURL”方法并不重要,因为 action.VIEW 不起作用。

你有什么解决办法吗?

【问题讨论】:

    标签: android android-intent webview android-manifest actionview


    【解决方案1】:

    我建议在您的活动中使用更简单的 WebView 布局。

    `<?xml version="1.0" encoding="utf-8"?>
    <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/my_webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />`
    

    在你的 WebViewActivity 中修改代码:

    String url = "https://www.google.com";
        setContentView(R.layout.your_layout);
        WebView webView = (WebView) findViewById(R.id.my_webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(url);
    

    【讨论】:

      【解决方案2】:

      Tnx 给所有人。 我找到了解决方案。 在 XML 中,我必须将其添加到意图过滤器:

      <data android:scheme="http" android:host="*"/>
      <data android:scheme="https" android:host="*"/>
      

      在 Activity 中我必须添加意图 URL:

      mURL = "";
      if (getIntent().getData() != null) {
      mURL = getIntent().getData().toString();
          setWebViewAndLoadURL(mURL);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-12
        • 2011-03-02
        • 2016-10-25
        • 2015-05-30
        • 1970-01-01
        • 1970-01-01
        • 2018-12-14
        相关资源
        最近更新 更多