Pass URL data from AppLink to WebView

HrishikeshK

New member
Aug 14, 2018
1
0
0
I am using App Links to allow users to open my app when they tap on my website's URL. I'd like to know, how can I load the URL in a WebView.

For example, if a user taps on a URL: https://www.domain.tld/page1.html, how can I pass that page1.html to a WebView activity? As of now, no matter what URL of my website the user is tapping on, the WebView activity is loading with the default index.html page. For a seamless experience, I'd like the URL that the user tapped on to open.

This is my SplashActivity.java (where I'm receiving the App Link Intent):

Code:
package com.application;

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;

    [MENTION=93274]overr[/MENTION]ide
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        Handler = new Handler();
        Handler.postDelayed(new Runnable()
                            {
                                [MENTION=93274]overr[/MENTION]ide
                                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();
    }
}

And this is my MainActivity.java (where I'm loading the WebView):

Code:
package com.application;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.preference.PreferenceManager;
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")

    [MENTION=93274]overr[/MENTION]ide
    protected void onCreate(Bundle savedInstanceState)
    {

        WebView wv = new WebView(this);
        wv.loadUrl("file:///android_asset/eula.html");
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setUserAgentString("customUA");
        wv.setWebViewClient(new WebViewClient()
        {
            [MENTION=93274]overr[/MENTION]ide
            public boolean shouldOverrideUrlLoading(WebView view, String url3)
            {
                view.loadUrl(url3);

                return true;
            }
        });

        final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean agreed = sharedPreferences.getBoolean("agreed",false);

        if(!agreed)
        {
            new AlertDialog.Builder(this, R.style.AlertDialog)
                    .setIcon(R.drawable.ic_remove_circle_black_24dp)
                    .setTitle(R.string.eula_title)
                    .setView(wv)
                    .setCancelable(false)
                    .setPositiveButton(R.string.accept, new DialogInterface.OnClickListener()
                    {
                        [MENTION=93274]overr[/MENTION]ide
                        public void onClick(DialogInterface dialog, int which)
                        {
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putBoolean("agreed", true);
                            editor.apply();
                        }
                    })
                    .setNegativeButton(R.string.decline, new DialogInterface.OnClickListener()
                    {
                        [MENTION=93274]overr[/MENTION]ide
                        public void onClick(DialogInterface dialog, int which)
                        {
                            finish();
                        }
                    })
                    .show();
        }

        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("customUA");
        WebView.setWebViewClient(new WebViewClient()
        {
            [MENTION=93274]overr[/MENTION]ide
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                LinearLayout.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            [MENTION=93274]overr[/MENTION]ide
            public void onPageFinished(WebView view, String url)
            {
                LinearLayout.setVisibility(View.GONE);
                super.onPageFinished(view, url);
                currentURL = url;
            }

            [MENTION=93274]overr[/MENTION]ide
            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();
            }

            [MENTION=93274]overr[/MENTION]ide
            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()
        {
            [MENTION=93274]overr[/MENTION]ide
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);
                ProgressBar.setProgress(newProgress);
            }
        });
    }

    [MENTION=93274]overr[/MENTION]ide
    public boolean onCreateOptionsMenu(Menu menu)
    {
        super.onPrepareOptionsMenu(menu);
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @SuppressLint("SetJavaScriptEnabled")

    [MENTION=93274]overr[/MENTION]ide
    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.update:
                Intent intent = new Intent(MainActivity.this, UpdateActivity.class);
                startActivity(intent);
                finish();
                break;

            case R.id.about:

                WebView wv2 = new WebView(this);
                wv2.loadUrl("file:///android_asset/about.html");
                wv2.getSettings().setJavaScriptEnabled(true);
                wv2.getSettings().setUserAgentString("customUA");
                wv2.setWebViewClient(new WebViewClient()
                {
                    [MENTION=93274]overr[/MENTION]ide
                    public boolean shouldOverrideUrlLoading(WebView view, String url4)
                    {
                        view.loadUrl(url4);

                        return true;
                    }
                });

                new AlertDialog.Builder(this, R.style.AlertDialog)
                        .setIcon(R.drawable.ic_info_black_24dp)
                        .setTitle(R.string.info)
                        .setView(wv2)
                        .setPositiveButton(R.string.okay, null)
                        .show();
                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()
                                {
                                    [MENTION=93274]overr[/MENTION]ide
                                    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();
        }
    }

    [MENTION=93274]overr[/MENTION]ide
    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()
                            {
                                [MENTION=93274]overr[/MENTION]ide
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    finish();
                                }
                            })
                    .setNegativeButton(R.string.no, null)
                    .show();
        }
    }
}
Please, do not point me to Android docs example. I have read that many times, but, didn't understand how to modify it to match my case. I'm a noob in this and most of the code that I've written is from Google searches.
 

Latest posts

Trending Posts

Members online

Forum statistics

Threads
958,194
Messages
6,976,052
Members
3,164,013
Latest member
Dave cat