Pass URL data from App Link to WebView

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Aug 15, 2018 4:13:18 PM via Website

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: mywebsite/page1.html (that's supposed to be a URL) 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):

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;

    @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();
    }
}

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

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")

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        WebView wv = new WebView(this);
        wv.loadUrl("a URL (can't post - new member)");
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setUserAgentString("customUA");
        wv.setWebViewClient(new WebViewClient()
        {
            @Override
            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()
                    {
                        @Override
                        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()
                    {
                        @Override
                        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("a URL (can't post - new member)");
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.getSettings().setUserAgentString("customUA");
        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("a URL (can't post - new member)"))
                {
                    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);
    }

    @SuppressLint("SetJavaScriptEnabled")

    @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.update:
                Intent intent = new Intent(MainActivity.this, UpdateActivity.class);
                startActivity(intent);
                finish();
                break;

            case R.id.about:

                WebView wv2 = new WebView(this);
                wv2.loadUrl("a URL (can't post - new member)");
                wv2.getSettings().setJavaScriptEnabled(true);
                wv2.getSettings().setUserAgentString("customUA");
                wv2.setWebViewClient(new WebViewClient()
                {
                    @Override
                    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()
                                {
                                    @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();
        }
    }
}

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.

Be the first to answer

Recommended articles