简体   繁体   English

Android深度链接未打开所需的链接

[英]Android Deep Linking not opening the required link

I want to associate my website ( https://freeairdrop.io ) with my app, such that when anyone gets a link to my website, it should prompt user to open the link in app(if installed) or open in browser(if app not installed) 我想将我的网站( https://freeairdrop.io )与我的应用程序关联,这样当任何人获得指向我网站的链接时,它都应提示用户在应用程序中打开链接(如果已安装)或在浏览器中打开(如果已安装)应用未安装)

This is AndroidManifest.xml file: 这是AndroidManifest.xml文件:

<intent-filter android:autoVerify="true">
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data
    android:scheme="https"
    android:host="freeairdrop.io" />
</intent-filter>

MyActivity.java file MyActivity.java文件

package app.freeairdrop.io;

import android.annotation.SuppressLint;
import....



public class MainActivity extends Activity{
    private ProgressBar progressBar;
    private WebView webView;
    private SwipeRefreshLayout mySwipeRefreshLayout;
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClientDemo());
        webView.setWebChromeClient(new WebChromeClientDemo());
        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

        webView.loadUrl("https://freeairdrop.io/");

        mySwipeRefreshLayout = (SwipeRefreshLayout) this.findViewById(R.id.swipeContainer);


        mySwipeRefreshLayout.setOnRefreshListener(
                new SwipeRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
                        webView.reload();
                        mySwipeRefreshLayout.setRefreshing(false);
                    }
                }
        );


        // ATTENTION: This was auto-generated to handle app links.
        Intent appLinkIntent = getIntent();
        String appLinkAction = appLinkIntent.getAction();
        Uri appLinkData = appLinkIntent.getData();
    }



    private class WebViewClientDemo extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Uri uri = Uri.parse(url);
            if (uri.getHost() != null && (url.startsWith("https://freeairdrop.io/") || url.startsWith("https://www.freeairdrop.io/"))) {
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            view.getContext().startActivity(intent);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }
    }


    private class WebChromeClientDemo extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        else {
//            finish();
        }
        return super.onKeyDown(keyCode, event);
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if (this.webView.canGoBack()) {
            this.webView.goBack();
            return;
        }
        Builder dialog = new Builder(this);
//        dialog.setTitle((CharSequence) "Exit App");
        dialog.setMessage((CharSequence) "Do You Want To Exit The App ?");
        dialog.setPositiveButton((CharSequence) "YES", (OnClickListener) new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.this.finish();
            }
        });
        dialog.setCancelable(false);
        dialog.setNegativeButton((CharSequence) "CANCEL", (OnClickListener) new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).show();
    }
}

By adding link through link assistant, whenever I click on sub links of my website ( https://freeairdrop.io/airdrop/discoin.html ), it prompts the user to open link in app, but it just opens the homepage( https://freeairdrop.io ). 通过链接助手添加链接,每当我单击我的网站的子链接( https://freeairdrop.io/airdrop/discoin.html )时,它都会提示用户打开应用程序中的链接,但只会打开主页( https ://freeairdrop.io )。

Deeplinking is pretty simple to do. 深层链接非常简单。

Grab the intent inside of your Activity, get the data from the intent, load it into the website. 抓住活动中的意图,从意图中获取数据,并将其加载到网站中。

Intent intent = getIntent();
Uri data intent.getData();

webview.loadURL(data);

This isnt tested, you may have to add your url before the data. 未经测试,您可能必须在数据之前添加网址。

webview.loadURL("URL_HERE" + data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM