Пользовательский браузер Android открывается дважды - PullRequest
0 голосов
/ 03 апреля 2019

Я пытался создать этот небольшой настраиваемый браузер, но у меня возникла небольшая проблема: когда я нажимаю на ссылку, она тихо открывается, но сначала я открываю пустую страницу, как будто это новый экземпляр действия. Фактически, когда я пытаюсь вернуться с открытой ссылки, я выполняю действие с пустой страницей. Это не меняет страницу, но активность. Что я делаю не так?

ДЕЯТЕЛЬНОСТЬ:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = "";
    if (getIntent().getData() != null) {
        setContentView(R.layout.activity_main);
        url = getIntent().getData().toString();
        WebView wv = (WebView) findViewById(R.id.webView);
        WebSettings settings = wv.getSettings();
        settings.setDomStorageEnabled(true);
        wv.clearCache(true);
        wv.clearHistory();
        settings.setJavaScriptEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        wv.setWebChromeClient(new WebChromeClient());
        wv.loadUrl(url);
    }

}

}

ПЛАН:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.andrea.webviewsample.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

МАНИФЕСТ:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.andrea.webviewsample">

<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:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:launchMode="singleTask">
    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <data android:scheme="http" />
            <data android:scheme="https" />
            <data android:host="*" />
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE" />
             <category android:name="android.intent.category.APP_BROWSER" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
</application>

</manifest>

ЭТО РЕЗУЛЬТАТ: Error in URL loading

1 Ответ

0 голосов
/ 03 апреля 2019

Решение найдено!

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = "";
    if (getIntent().getData() != null) {
        WebView wv = new WebView(this);
        url = getIntent().getData().toString();
        WebSettings settings = wv.getSettings();
        settings.setDomStorageEnabled(true);
        wv.clearCache(true);
        wv.clearHistory();
        settings.setJavaScriptEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
        wv.loadUrl(url);
        setContentView(wv);
    }
}
...