深度链接仅打开一个应用程序实例?

时间:2019-11-16 15:17:16

标签: android deep-linking

关闭应用程序后,深层链接可以正常工作。但是,如果该应用程序已经打开,则Deeplink只会打开该应用程序,而不是打开该链接。

MainActivity.java文件:

public class MainActivity extends Activity{
    private ProgressBar progressBar;
    private WebView webView;
    private SwipeRefreshLayout mySwipeRefreshLayout;
    private boolean mShouldPause;

    @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().setDomStorageEnabled(true);
        webView.clearCache(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);

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


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



    private class WebViewClientDemo extends WebViewClient {



        @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 {
        }
        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;
        }

        else {
            // Let the system handle the back button
           super.onBackPressed();
        }
    }

}

假设我通过电报打开了应用链接(当应用未运行时),它可以完美打开。但是,当应用程序在后台运行时,然后单击Telegram中的应用程序链接时,它只是打开应用程序,而不是打开该特定链接。

1 个答案:

答案 0 :(得分:0)

当应用程序在bacground或前景中运行时。

  • 在清单中为活动添加应用启动模式singleTop
  • 在活动句柄中,打开onNewintent(Intent)中的链接URL
    protected void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       Uri uri = intent.getData();
       if (uri != null && uri.toString().startsWith("http://") 
                           || uri.toString().startsWith("https://")) {
           //open the link here
       }
    }