捕获Intent提供的数据

时间:2018-12-04 11:23:20

标签: java android url android-intent applinks

在此页面上:https://developer.android.com/training/app-links/deep-linking,在

  

“从传入意图中读取数据”

部分,Google提到:

  

系统一旦通过意图过滤器开始您的活动,您就可以   使用Intent提供的数据来确定您需要呈现的内容。   调用getData()和getAction()方法以检索数据并   与传入的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 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();
    }
}

我想知道(以后将其转移到另一个活动中)单击打开我的应用程序所用的URL。

基本上,我的应用程序正在使用应用程序链接(某些人称为“深层链接”)。因此,如果用户“ A”向用户“ B”发送了我网站上的页面链接,并且B安装了我的应用程序,则他/她将能够在我的应用程序中打开链接,而不是在浏览器中打开。

到目前为止,B可以在我的应用程序中打开链接,但是,我的应用程序将始终加载“主页”(这是因为我已将我的应用程序编码为默认情况下打开主页)。我想加载将B带入应用程序的页面。

对于一个真实的例子,就像Facebook的应用程序一样。假设我将一个Facebook页面或个人资料的链接共享给我的朋友。这是我的网络浏览器中显示的标准HTTP链接。可能是我在WhatsApp上与他分享的。他触摸链接以打开它。他的手机上安装了官方的Facebook应用程序。因此,Android询问他是否要在Facebook应用程序或浏览器中打开链接。现在,当他选择浏览器时,完全没有问题。但是,当他选择Facebook应用程序时,该应用程序会加载链接上存在的配置文件或页面,而不是Facebook主页。这就是我想要实现的。

请注意,我在这里谈论的是标准HTTP链接,而不是我的应用程序特定的URI。

更新:

这是我现在尝试的内容:

这与上面的活动相同,我对此进行了修改:

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


    }
}

以及我正在加载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();
        }
    }
}

但是,我在Cannot resolve symbol url遇到WebView.loadUrl(R.string.url);错误

该怎么办?

0 个答案:

没有答案
相关问题