android自定义url方案..? [英] android custom url scheme..?

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

问题描述

我正在尝试创建自己的 url 方案,以便可以通过 URL 调用我的 android 应用程序,但目前我没有成功.

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>

有谁可以帮我告诉我出了什么问题?另外,如果有人发现出了什么问题,有人能告诉我我是如何从我的应用程序的 android 代码中读取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 变量值.这是我试过的代码.

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");
}

但是我从所有函数中收到 null ......我还能做什么?

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"

但在我的情况下:mySchememyBundlemyURI 被设置为 null.

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.

推荐答案

我认为问题出在您定义的 Action 上.有一个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>

试试那个,我敢打赌会正确解决.我之所以做出这个假设,是因为您包含了浏览器通常使用的可浏览类别,它不知道您的任何自定义操作.如果您确实想要 GALLERY 操作,就像您暗示的那样,那么只需创建 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方案..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆