简体   繁体   English

如何阻止 URL 以打开我的 android 应用程序?

[英]How to block URL to open my android app?

My android app allows to open other website like " http://www.google.com " in a web browser from app.我的 android 应用程序允许在应用程序的网络浏览器中打开其他网站,如“ http://www.google.com ”。 And my code is below.我的代码如下。

<activity
    android:name=".HomeActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" >
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />

        <data android:scheme="http" />
        <data android:scheme="https" />
    </intent-filter>
</activity>

But this gives me a problem.但这给我带来了问题。 In the email or text message, click any website link, there will be a choice to open from my app.在电子邮件或短信中,单击任何网站链接,将有一个从我的应用程序打开的选项。 See the picture I posted.看我发的图片。 If I click on www.google.com in the text message, I will get a choice to open from my app.如果我点击短信中的 www.google.com,我会选择从我的应用程序打开。

I don't want the popup shows my app.我不希望弹出窗口显示我的应用程序。 How to handle this?如何处理? Please help.请帮忙。 Thanks a lot.非常感谢。

在此处输入图片说明

You can't avoid that, unless you specifically know what browser the user wants to use.您无法避免这种情况,除非您特别了解用户想要使用的浏览器。 And you don't know that.而你不知道。

You could have a WebView activity/fragment bundled in your application that will display webpages for you.您可以在应用程序中捆绑一个WebView活动/片段,为您显示网页。 That way you'd have full control.这样你就可以完全控制了。 Here's a guide on WebView这是guide on WebViewguide on WebView

Your app is being displayed in the Complete Action Using chooser because of your intent-filter:由于您的意图过滤器,您的应用程序显示在使用选择器的完整操作中

<intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.VIEW" />

            <data android:scheme="http" />
            <data android:scheme="https" />
        </intent-filter>

and particularly because you've specified just a "scheme" entry, with no specific "host".尤其是因为您只指定了一个“方案”条目,而没有特定的“主机”。 This would in effect offer any web URL intent to open in your app.这实际上会提供在您的应用程序中打开的任何 Web URL 意图。

Without understanding what your intended behavior exactly is, you can delete the <data> nodes from your intent-filter to remove your app from the Chooser.在不了解您的预期行为究竟是什么的情况下,您可以从意图过滤器中删除 <data> 节点以从选择器中删除您的应用程序。

Or alternatively, you might want specific URL's to offer your app in the Chooser, like http://yourserver.com and https://yourserver.com , for this you could alter your intent-filter as follows:或者,您可能希望特定 URL 在选择器中提供您的应用程序,例如http://yourserver.comhttps://yourserver.com ,为此您可以按如下方式更改您的意图过滤器:

<data android:scheme="http" android:host="yourserver.com" />

If your question is to block URLs of other websites in your android app for download purpose then search for the iConstants.java file in your project and write the following code:如果您的问题是为了下载目的而在您的 android 应用程序中阻止其他网站的 URL,请在您的项目中搜索iConstants.java文件并编写以下代码:

public interface iConstants {
    public static final String[] DISABLE_DOWNLOADING = {"URL", };
    public static final String WEB_DISABLE = "We cannot allow to download videos form this website.";

}

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

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