Capture the data provided by Intent

…衆ロ難τιáo~ 提交于 2020-01-14 02:28:12

问题


On this page: https://developer.android.com/training/app-links/deep-linking, in the

'Read data from incoming intents'

section, Google mentions:

Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent.

And that's exactly what I'm trying to do, but, I'm unable to get help.

In this activity of mine:

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 SplashActivity3 extends Activity
{
    Handler Handler;

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

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

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

I want to know (later tranfer it to another activity), the URL that was clicked on to open my app.

Basically, my app is using App Links (referred to as Deep Linking by some). So, in a case where a user 'A', sends user 'B' a link of a page on my website and B has my app installed, he/she will be able to open the link in my app instead of the browser.

As of now, B can open the link in my app, but, my app will always load the 'home page' (it's because I have coded my app to open the home page by default). I want to load the page that brought B in my app.

For a real life example, just like Facebook's app does. Suppose I share a link of a Facebook page or a profile to my friend. It's a standard HTTP link that's displayed in my web browser. Probably, I shared it with him on WhatsApp. He touches the link to open it. He has the official Facebook app installed on his phone. So, Android asks him if he wants to open the link in the Facebook app or in the browser. Now, when he chooses the browser, it's no problem at all. But, when he chooses the Facebook app, the app loads the profile or the page that exists on the link and not the home page of Facebook. That's what I want to achieve.

Please note, I'm talking about standard HTTP links here and not my app specific URIs.

UPDATE:

Here's something I tried now:

This is the same activity as above, I modified it like this:

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 SplashActivity3 extends Activity
{
    Handler Handler;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        final Intent appLinkIntent = getIntent();
        final String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();

        final Bundle bundle = new Bundle();
        bundle.putString("web", appLinkAction);

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

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


    }
}

And the activity in which I'm loading the URL:

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 WebViewActivity 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("file:///android_asset/eula.html");
        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.webView5);
        ProgressBar = findViewById(R.id.progressBar5);
        LinearLayout = findViewById(R.id.layout5);

        ProgressBar.setMax(100);

        Bundle bundle = getIntent().getExtras();
        String url = bundle.getString("web");

        WebView.loadUrl(R.string.url);
        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(WebViewActivity.this, ErrorActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url2)
            {
                if (url2.contains("www.mydomain.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);
    }

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

But, I'm getting Cannot resolve symbol url error at WebView.loadUrl(R.string.url);

What to do?

来源:https://stackoverflow.com/questions/53611882/capture-the-data-provided-by-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!