从webview Android应用程序中的浏览器打开链接

时间:2017-09-20 02:31:40

标签: android android-intent webview deeplink

所以我按照post的说明进行操作,但仍然无法让我的应用程序打开应用程序内的链接。

我做了很多研究。请记住,我是开发android的新手。谁能帮我?

如果有人点击某个链接(例如:styleonem.co/about)转到该页面中的该网址,我想要做的就是打开一个意图。

现在发生的是它打开应用程序(好!)但它从不在Webview中加载URL。我以为我把它全部排序了,但我一直在寻找并试图学习这个新的应用程序界面几个星期,我仍然无法弄明白。

以下是我的代码

的AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="co.styleonem.styleclothingco">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                  android:configChanges="orientation|screenSize"
                  android:launchMode="singleInstance"
                  android:alwaysRetainTaskState="true">
            <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.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="https" android:host="styleonem.co"/>


                <data android:scheme="http" android:host="www.styleonem.co"/>
            </intent-filter>

        </activity>
    </application>

</manifest>

MainActivity.java:

package co.styleonem.styleclothingco;

import android.app.Application;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.onesignal.OSNotification;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;

import org.json.JSONObject;

import javax.sql.RowSet;

import static android.R.attr.data;


public class MainActivity extends AppCompatActivity {


    private WebView mywebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mywebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings= mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        // Line of Code for opening links in app
        mywebView.setWebViewClient(new WebViewClient());
        mywebView.setWebViewClient(new HelloWebViewClient());
        CookieManager.getInstance().setAcceptCookie(true);
        final WebView webview = (WebView)findViewById(R.id.webview);


        webview.getSettings().setUserAgentString("style-co-app");


        mywebView.loadUrl("https://styleonem.co/");
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

        OneSignal.startInit(this).setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
            @Override
            public void notificationOpened(OSNotificationOpenResult result) {

                String launchURL = result.notification.payload.launchURL;


                if (launchURL != null) {
                    Log.d(Const.DEBUG, "Launch URL: " + launchURL);

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("url", launchURL);
                    startActivity(intent);

                } else {
                    Log.d(Const.DEBUG, "Launch URL not found");

                    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        }).init();



    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {








            if (Uri.parse(url).getHost().equals("styleonem.co")) {
                // This is my web site, so do not override; let my WebView load the page
                return false;

            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);

            Uri data = getIntent().getData();
            String extUrl = toString();

            if (data != null) {
                mywebView.loadUrl(toString());
            } else {
                mywebView.loadUrl(url);
            }

            return true;

        }
    }


    //Code For Back Button
    @Override
    public void onBackPressed() {
        if(mywebView.canGoBack())
        {
            mywebView.goBack();
        }

        else
        {
            super.onBackPressed();
        }
    }

}

0 个答案:

没有答案