在WebView中加载AppLinkIntent URL

时间:2018-07-22 05:47:47

标签: java android android-intent webview

我有MainActivity,可将我的网站加载到WebView中。网站的所有链接都在WebView本身中打开。另外,我添加了一个AppLink,允许用户将我的网站加载到我的应用中。

现在发生的事情是,无论用户在另一个应用程序(例如,https://www.domain.tld/page.html)上触摸了哪个URL,他/她都可以打开仅加载我的首页(在这种情况下,{{3} }。问题是,我想在用户正在触摸的WebView中加载确切的URL(如上所述,https://www.domain.tld/index.html)。

这是我的MainActivity.java:

package com.app.packagename;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressWarnings("deprecation")

public class MainActivity extends AppCompatActivity
{
    private WebView WebView;
    private ProgressBar ProgressBar;
    private LinearLayout LinearLayout;
    private String currentURL;

    @SuppressLint("SetJavaScriptEnabled")


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

        WebView = findViewById(R.id.webView);
        ProgressBar = findViewById(R.id.progressBar);
        LinearLayout = findViewById(R.id.layout);

        ProgressBar.setMax(100);

        WebView.loadUrl("https://www.domain.tld/index.html");
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.getSettings().setUserAgentString("customUserAgent");
        WebView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                LinearLayout.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                LinearLayout.setVisibility(View.GONE);
                super.onPageFinished(view, url);
                currentURL = url;
            }

            @Override
            public void onReceivedError(WebView webview, int i, String s, String s1)
            {
                WebView.setVisibility(View.GONE);

                Intent intent = new Intent(MainActivity.this, ErrorActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url2)
            {
                if (url2.contains("www.domain.tld"))
                {
                    view.loadUrl(url2);
                    return false;
                } else
                {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url2));
                    startActivity(intent);
                    return true;
                }
            }

        });

        WebView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);
                ProgressBar.setProgress(newProgress);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onPrepareOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.backward:
                onBackPressed();
                break;

            case R.id.forward:
                onForwardPressed();
                break;

            case R.id.refresh:
                WebView.reload();
                break;

            case R.id.share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT,currentURL);
                startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.shareWith)));
                break;

            case R.id.exit:
                new AlertDialog.Builder(this,R.style.AlertDialog)
                        .setIcon(R.drawable.ic_error_black_24dp)
                        .setTitle(R.string.title)
                        .setMessage(R.string.message)
                        .setPositiveButton(R.string.yes,
                                new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        finish();
                                    }
                                })
                        .setNegativeButton(R.string.no, null)
                        .show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onForwardPressed()
    {
        if (WebView.canGoForward())
        {
            WebView.goForward();
        } else
        {
            Toast.makeText(this, R.string.noFurther, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed ()
    {
        if (WebView.canGoBack())
        {
            WebView.goBack();
        } else
        {
            new AlertDialog.Builder(this,R.style.AlertDialog)
                    .setIcon(R.drawable.ic_error_black_24dp)
                    .setTitle(R.string.title)
                    .setMessage(R.string.message)
                    .setPositiveButton(R.string.yes,
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    finish();
                                }
                            })
                    .setNegativeButton(R.string.no, null)
                    .show();
        }
    }
}

我在SplashActivity.java中收到AppLink:

package com.app.packagename;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

@SuppressWarnings("unused")

public class SplashActivity extends Activity
{
    Handler Handler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        Handler = new Handler();
        Handler.postDelayed(new Runnable()
                            {
                                @Override
                                public void run()
                                {
                                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }
                            },
                1500);

        Intent
                appLinkIntent =
                getIntent();
        String
                appLinkAction =
                appLinkIntent.getAction();
        Uri
                appLinkData =
                appLinkIntent.getData();
    }
}

0 个答案:

没有答案