Android的自定义URL方案

Android的自定义URL方案

本文介绍了Android的自定义URL方案..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去创建自己的URL方案,所以我的Andr​​oid应用程序可以通过一个URL,但现在被调用我没有成功。

Im trying to create my own url scheme so my android app can get called via an URL but for now I dont have a success.

我试着让这个网址的工作:cedemo://com.cedemo.scan X = TOTO

Im trying to have this url to work : cedemo://com.cedemo.scan?X=toto

下面是我的清单文件的一部分:

Here is part of my manifest file :

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.GALLERY" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

有谁可以帮告诉我什么是错的?另外,如果有人发现什么是错的,有人可以告诉我怎样从我的应用程序读取里面的机器人code中的X变量?

Does anyone can help telling me what is wrong ?Also, if someone find what is wrong, can someone tell me how I read the "X" variable from inside the android code from my app ?

更新:

更新:我做的动作的修改(如建议的答案之一),它的正常工作。问题是,我仍然不能获得url变量的值。这里是code我试过了。

Update: I did the modification of the action (as advised in one of the answers) and it's worked fine. The thing is that I still cannot get the url variable value. Here is the code I tried.

final Intent intent = getIntent();
final String myScheme=intent.getScheme();
final Bundle myBundle=intent.getExtras();
final boolean inContestKey;
if (myBundle != null) {
    inContestKey=myBundle.containsKey("inContest");
}
final Uri myURI=intent.getData();
final String value;
if (myURI != null) {
    value = myURI.getQueryParameter("inContest");
}

不过,我收到从所有的功能...我还能做什么?

But I receiving null from all the functions… what else can i do?

也许我应该解释一下我的软件更好的环境:

May be I should explain better the context of my software:

  1. 在我的软件启动
  2. 在我的软件推出,然后在浏览器
  3. 在用户点击一个链接,浏览器和浏览器进​​入URL方案,回用变量X的软件(例如)
  4. 软件应读取变量X

但是,在我的情况: myScheme myBundle myURI 设置为

But in my case : myScheme, myBundle, myURI are set to null.

任何想法?

更新:

我找到了答案是,你必须要在主要活动做到这一点。

I found the answer is that you have to be in the main activity to do that.

推荐答案

我认为这个问题是与你所定义的动作。有一个android.intent.action.VIEW这是我想你想要的。

I think the problem is with the Action you defined.There is a "android.intent.action.VIEW" which is what I think you want.

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <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="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

尝试,我敢打赌会正确解析。我只是因为你包括它是常用的浏览器,不知道您的任何自定义操作可浏览的类别作出这个假设。如果你想要的画廊行动,你已经暗示然后就创建2个过滤器

Try that and I bet will resolve correctly. I only made this assumption because you included the browsable category which is usually used by the Browser, which does not know of any of your custom actions. If you do want the GALLERY action as you have implied then just create 2 filters

<activity android:name=".Gallery1" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.GALLERY" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="cedemo" android:host="com.cedemo.scan" />

        </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:scheme="cedemo" android:host="com.cedemo.scan" />

        </intent-filter>
</activity>

所以,你的活动内容中,你可以这样做:

So within the contents of your activity you can do something like:

// Value should be "toto" as in your example
String value = getData().getQueryParameter("X");

这篇关于Android的自定义URL方案..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:30