【问题标题】:Deep linking opens the app but does not open the exact url深层链接会打开应用程序,但不会打开确切的 url
【发布时间】:2017-10-21 10:22:32
【问题描述】:

我已经制作了一个 WebView 应用程序,并且可以在上面进行深度链接。每当有人单击指向我的网站的链接(在任何其他应用程序上)时,我的应用程序就会打开,但是会加载主页而不是单击的 URL。我希望在点击链接而不是主页时打开我网站的确切 URL。

MainActivity.java

package com.yoalfaaz.yoalfaaz;

        import android.content.Intent;
        import android.os.Bundle;
        import android.support.design.widget.FloatingActionButton;
        import android.support.design.widget.Snackbar;
        import android.support.v4.widget.SwipeRefreshLayout;
        import android.support.v7.app.AppCompatActivity;
        import android.support.v7.widget.ShareActionProvider;
        import android.support.v7.widget.Toolbar;
        import android.view.View;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.webkit.WebSettings;
        import android.webkit.WebView;
        import android.webkit.WebViewClient;
        import android.support.v4.view.MenuItemCompat;

public class MainActivity extends AppCompatActivity {
    private WebView YoWeb;
    private ShareActionProvider mShareActionProvider;

    SwipeRefreshLayout swipe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        YoWeb = (WebView)findViewById(R.id.webview); // Move your declaration up here
        swipe = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                LoadWeb(YoWeb.getUrl());  // Pass in the current url to refresh
            }
        });

        LoadWeb("https://www.yoalfaaz.com");  // load the home page only once
    }

    public void LoadWeb(String url) // Pass in URL you want to load
    {

        WebSettings webSettings = YoWeb.getSettings();
        webSettings.setJavaScriptEnabled(true);
        YoWeb.loadUrl(url);  // Load the URL passed into the method
        swipe.setRefreshing(true);
        YoWeb.setWebViewClient(new WebViewClient() {

            //onPageFinished Method
            public void onPageFinished(WebView view, String url) {
                //Hide the SwipeRefreshLayout
                swipe.setRefreshing(false);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (YoWeb.canGoBack()) {
            YoWeb.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) {
            case R.id.action_settings:
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate menu resource file.
        getMenuInflater().inflate(R.menu.menu_main, menu);

        // Locate MenuItem with ShareActionProvider
        MenuItem item = menu.findItem(R.id.menu_item_share);

        // Fetch and store ShareActionProvider
        mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);

        // Return true to display menu
        return true;
    }

    // Call to update the share intent
    private void setShareIntent(Intent shareIntent) {
        if (mShareActionProvider != null) {
            mShareActionProvider.setShareIntent(shareIntent);
        }
    }

    //private Intent setShareIntent() {
    //    Intent shareIntent = new Intent();
    //    shareIntent.setAction(Intent.ACTION_SEND);
    //    shareIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
    //    shareIntent.setType("text/plain");
    //    startActivity(shareIntent);
    //    return shareIntent;
    //}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yoalfaaz.yoalfaaz">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter >
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="www.yoalfaaz.com" android:scheme="https"/>
            </intent-filter>
        </activity>
        <activity android:name=".SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如何使它与大多数其他应用程序一起工作?就像我们点击 WhatsApp 中的 YouTube 链接一样,该特定视频会在 YouTube 应用而不是 YouTube 主页中打开。

【问题讨论】:

  • LoadWeb("https://www.yoalfaaz.com"); // load the home page only once。嗯,打开你的主页不是吗?
  • @greenapps 是的,它会打开我的主页。这是因为每次我在我网站的任何网页上滑动刷新时,我都会回到首页。
  • Whenever someone clicks a link to my website (on any other app) 哪个应用程序?你的意思是一些浏览器应用程序?
  • 是的,那是您的主页网址。所以把点击的 url 放到那个页面上。
  • @greenapps “哪个应用程序?”具体来说,我的意思是说WhatsApp。如果我在 WhatsApp 上点击我网站的链接,它会打开我的网站,但不会打开确切的 URL。

标签: java android android-webview deep-linking


【解决方案1】:
LoadWeb("https://www.yoalfaaz.com");  // load the home page only once

不要对 URL 进行硬编码,而是使用 getIntent().getData().toString() 获取从其他应用程序单击的 URL(作为 string)并使用此 URL 加载 Web 视图。

如果没有从getData() 接收到 URL,请使用 null 检查并加载默认 URL。

希望这会有所帮助。

========= 已编辑以按您的要求显示代码 =====================

在您的代码中,您正在使用这一行加载 web 视图。

LoadWeb("https://www.yoalfaaz.com");  // load the home page only once

如下修改这一行。

String url = "https://www.yoalfaaz.com";
if(null != getIntent().getData()){
url = getIntent().getData().toString();
}
LoadWeb(url);

【讨论】:

  • 你能告诉我应该如何编码以将 URL 作为字符串而不是像我那样做吗?
  • 已编辑答案,向您展示如何获取 url 的代码。看看吧。
  • getIntent().getData() 无论如何都会返回null 给我
猜你喜欢
  • 1970-01-01
  • 2016-10-21
  • 2016-05-10
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 2022-01-08
  • 2021-01-26
  • 1970-01-01
相关资源
最近更新 更多