Java 如何注册一些 URL 命名空间 (myapp://app.start/) 以通过在 Android 操作系统中的浏览器中调用 URL 来访问您的程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2430045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:30:48  来源:igfitidea点击:

How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?

javaandroidbrowser

提问by Rella

So I want to create an Android app so it would be registered somewhere in android OS (or just would start on system start) and when phone user clicks on special button on a web page inside a web browser a la:

所以我想创建一个 Android 应用程序,以便它可以在 android 操作系统中的某个地方注册(或者只是在系统启动时启动),并且当电话用户单击 Web 浏览器内网页上的特殊按钮时:

 <a href="myapp://mysettings">Foo</a> 

my app would pop up and run using the params sent in that URL.

我的应用程序将使用该 URL 中发送的参数弹出并运行。

So how do I do such thing?

那么我该怎么做呢?

I need a tutorial with code!

我需要一个带代码的教程!

采纳答案by hackbod

You need to follow the standard rules for URIs via the W3C and such, which basically means: do not do this.

您需要通过 W3C 等遵循 URI 的标准规则,这基本上意味着:不要这样做。

Android defines a Uri syntax for describing a generic Intent. There are methods on Intent for converting to and from this representation, such as: http://developer.android.com/reference/android/content/Intent.html#toUri(int)

Android 定义了用于描述通用 Intent 的 Uri 语法。在 Intent 上有一些方法可以与这种表示进行转换,例如:http: //developer.android.com/reference/android/content/Intent.html#toUri(int)

So the way to do this is to use the normal facilities to describe an in your manifest for the kinds of intents you are going to handle with a particular component, especially defining an action name in your own namespace (com.mycompany.myapp.action.DO_SOMETHING or whatever). You can then make an Intent that matches your component, and use Intent.toUri() to get the URI representation of this. This can be placed in your link, and will then when pressed look for something that handles and and thus find your app. Note to be launched from the browser like this, the component's must handle the BROWSABLE category. (You don't need to have this in the Intent you put in the link, the browser will automatically add this in for you.)

因此,这样做的方法是使用常规工具在清单中描述您将使用特定组件处理的意图类型,尤其是在您自己的命名空间(com.mycompany.myapp.action .DO_something 或其他)。然后,您可以创建一个与您的组件匹配的 Intent,并使用 Intent.toUri() 获取它的 URI 表示。这可以放置在您的链接中,然后在按下时查找可以处理的内容,从而找到您的应用程序。注意要像这样从浏览器启动,组件必须处理 BROWSABLE 类别。(你不需要在你放在链接中的 Intent 中有这个,浏览器会自动为你添加这个。)

Finally, you may want to set the package of the intent to your app with this: http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

最后,您可能希望通过以下方式将意图的包设置为您的应用程序:http: //developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)

This is a newer feature in the platform, which allows you to direct link intents to only your app so that other applications can not intercept and handle them.

这是平台中较新的功能,它允许您将链接意图仅指向您的应用程序,以便其他应用程序无法拦截和处理它们。

In summary: read the regular documentation on intents and intent filters (such as the NotePad tutorial, though you won't be using content: URIs here, probably just custom actions) and get your app working that way. Then you can make a browser link to launch your app in the same way, provided your intent-filter handles the BROWSABLE category.

总结:阅读有关意图和意图过滤器的常规文档(例如 NotePad 教程,尽管您不会在这里使用内容:URI,可能只是自定义操作)并让您的应用程序以这种方式工作。然后您可以创建一个浏览器链接以相同的方式启动您的应用程序,前提是您的意图过滤器处理 BROWSABLE 类别。

回答by 9re

First, to be able to start your app from link with custom scheme 'myapp' in browser / mail, set intent filter as follows.

首先,为了能够在浏览器/邮件中使用自定义方案“myapp”的链接启动您的应用程序,请按如下方式设置意图过滤器。

<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:scheme="myapp"/> 
</intent-filter>

and to parse queries in your link myapp://someaction/?var=str&varr=string
(the code is over simplified and has no error checking.)

并解析链接 myapp://someaction/?var=str&varr=string 中的查询
(代码过于简化,没有错误检查。)

Intent intent = getIntent();
// check if this intent is started via custom scheme link
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
  Uri uri = intent.getData();
  // may be some test here with your custom uri
  String var = uri.getQueryParameter("var"); // "str" is set
  String varr = uri.getQueryParameter("varr"); // "string" is set
}

[edit] if you use custom scheme to launch your app, one of the problem is that: The WebView in another apps may not understand your custom scheme. This could lead to show 404 page for those browser for the link with custom scheme.

[编辑] 如果您使用自定义方案来启动您的应用程序,问题之一是:其他应用程序中的 WebView 可能无法理解您的自定义方案。这可能会导致浏览器显示 404 页面以获取自定义方案的链接。

回答by Jason Van Anden

Here is my cut to the chase contribution.

这是我切入正题的贡献。

Create an intent filter in the activity you want to load in the manifest like this:

在要加载到清单中的活动中创建一个意图过滤器,如下所示:

<intent-filter>
    <action android:name="com.bubblebeats.MY_CUSTOM_ACTION" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

Your web page URL for this will look like this:

您的网页 URL 将如下所示:

intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end

The most basic HTML code to launch your apk from a browser would look like this:

从浏览器启动您的 apk 的最基本的 HTML 代码如下所示:

<body>
    <a href="intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;end">click to load apk</a>
</body>

To add variables to your intent

为您的意图添加变量

You can generate the URI from within your Android code like this:

您可以在 Android 代码中生成 URI,如下所示:

Intent i = new Intent();

i.setAction("com.bubblebeats.MY_CUSTOM_ACTION");
i.putExtra("some_variable", "123456");

Log.d("ezpz", i.toUri(Intent.URI_INTENT_SCHEME));

This will produce this:

这将产生这个:

04-13 09:47:30.742: DEBUG/ezpz(9098): intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end

You just want this part:

你只想要这部分:

intent:#Intent;action=com.bubblebeats.MY_CUSTOM_ACTION;S.some_variable=123456;end

Take a good look at what occurred here and you can see how you can skip the code step and manually create these URIs yourself.

仔细看看这里发生了什么,您会看到如何跳过代码步骤并自己手动创建这些 URI。

Especially this part:

特别是这部分:

S.some_variable=123456

回答by AlbAtNf

Because hackbod never gave us code-examples, I just want to share mine, after I got this to work.

因为 hackbod 从来没有给我们代码示例,所以我只想在我开始工作后分享我的代码示例。

First of all, you need to define a custom action in your manifest file:

首先,您需要在清单文件中定义一个自定义操作:

<activity
    android:name=".activity.MainActivity"
    android:label="@string/app_name_full">
    <intent-filter>
        <action android:name="com.yourpackage.action.OPEN_VIEW"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <category android:name="android.intent.category.BROWSABLE"></category>
    </intent-filter>
</activity>

Then, for the content on your website, you need to generate the URI from an intent. Put following code in your Activity (This code could be removed, once the link is generated):

然后,对于您网站上的内容,您需要从意图生成 URI。将以下代码放在您的活动中(一旦生成链接,此代码可以删除):

Intent i = new Intent();
        i.setAction("com.yourpackage.action.OPEN_VIEW");
        i.setPackage("com.yourpackage");
        i.putExtra("myextra","anystring");
        Log.d(getClass().getSimpleName(), i.toUri(Intent.URI_INTENT_SCHEME));

To receive the Extras, put following in your activity, that is able to recieve the custom action (as defined in manifest):

要接收 Extras,请将以下内容放入您的活动中,以便能够接收自定义操作(如清单中所定义):

final Intent intent = getIntent();
final String action = intent.getAction();

        if ("com.yourpackage.action.OPEN_VIEW".equals(action)) {
           Log.i(getClass().getSimpleName(), "EXTRA: "+intent.getExtras().getString("myextra"));
        }

On your website (this is the previously generated link):

在您的网站上(这是之前生成的链接):

<a href="intent:#Intent;action=com.yourpackage.action.OPEN_VIEW;package=com.yourpackage;S.myextra=anystring;end">Open App with extra</a>

Hope that helps someone for better understanding. Please correct me, if I got something wrong.

希望能帮助别人更好地理解。请纠正我,如果我有什么不对的地方。