简体   繁体   English

Android Intent从浏览器启动

[英]Android Intent launch from browser

This topic has been covered before, but I can't find an answer specific to what I'm asking. 之前已经讨论过这个话题,但我找不到具体针对我所要求的答案。

Where I am: I followed hackmod's first piece of advice here: Make a link in the Android browser start up my app? 我在哪里:我在这里遵循hackmod的第一条建议: 在Android浏览器中建立一个链接启动我的应用程序? and got this to work with a link in the webpage. 并使用网页中的链接。

However, I'm having trouble understanding the second option (intent uri's). 但是,我无法理解第二个选项(意图uri)。 here's what I've got: 这是我得到的:

    <activity android:name="com.myapps.tests.Layout2"
        android:label="Auth Complete"
        >
        <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="http" android:host="mydomain.com"
                android:path="/launch" />
        </intent-filter>
    </activity>

Now, with that I can go to "mydomain.com/launch" and it launches my activity. 现在,我可以去“mydomain.com/launch”,它启动我的活动。 this all works well, except that I get the chooser. 这一切都运作良好,除了我得到选择器。 what I want is for it to just launch my activity without giving options. 我想要的是它只是在没有提供选项的情况下启动我的活动。

From the explanation in the post I referenced it looks like thats what intent uris are for,but I can't find a straightforward example. 从我引用的帖子中的解释看起来就像是uris的意图,但我找不到一个直截了当的例子。 what should my link in my webpage look like in order to launch this with no chooser? 我的网页中的链接应该是什么样的,以便在没有选择器的情况下启动它?

I've seen a couple of examples that look something like this: 我看过几个看起来像这样的例子:

<a href="intent:#Intent;action=com.myapp.android.MY_ACTION;end">

However, that doesn't seem to work when I try it. 但是,当我尝试它时似乎不起作用。

My test device is a Galaxy Tab 2. 我的测试设备是Galaxy Tab 2。

any help would be appreciated. 任何帮助,将不胜感激。

I was also trying to launch the app in the recomended way. 我也试图以推荐的方式启动应用程序。 The following code worked for me. 以下代码对我有用。

Code inside the <activity> block of YourActivity in AndroidManifest.xml : AndroidManifest.xml中YourActivity<activity>块内的代码:

<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
    <action android:name="your.activity.namespace.CUSTOMACTION" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

Code in the activities onCreate() method : 活动onCreate()方法中的代码:

Intent intent = getIntent();
if(intent != null && intent.getAction() == "your.activity.namespace.CUSTOMACTION") {
    extraValue = intent.getStringExtra("extraValueName");
    Log.e("AwseomeApp", "Extra value Recieved from intent : " + extraValue);
}

For the code in HTML, write an intent for launching the specific Activity, with the action your.activity.namespace.CUSTOMACTION , and your application package name your.activity.namespace . 对于HTML中的代码,编写启动特定Activity的意图,使用操作your.activity.namespace.CUSTOMACTION ,并将应用程序包命名为your.activity.namespace Your can also put some extra in the intent. 你也可以在意图中增加一些额外的东西。 For example intent.putExtra("extraValueName", "WOW") . 例如, intent.putExtra("extraValueName", "WOW") Then get the required URL by printing the value of intent.toUri(Intent.URI_INTENT_SCHEME) in the Log. 然后通过在Log中打印intent.toUri(Intent.URI_INTENT_SCHEME)的值来获取所需的URL。 It should look like : 它应该看起来像:

intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end

So your HTML code should look like : 所以你的HTML代码应如下所示:

<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.activity.YourActivity;S.extraValueName=WOW;end">
    Launch App
</a>

This is as per what @hackbod suggested in here and this page from developers.google.com. 这是@hackbod在此处以及来自developers.google.com的页面中提供的内容。

Depending on your intent filter this link should work: 根据您的意图过滤器,此链接应该有效:

<a href="http://mydomain.com/launch">start my app</a>

But you should note that the android system will ask the user if your app or any other browser should be started. 但是你应该注意到android系统会询问用户你的应用程序或任何其他浏览器是否应该启动。

If you want to avoid this implement a custom protcol handler. 如果你想避免这个实现一个自定义的protcol处理程序。 So just your app will listen for that and the user won't get the intent chooser. 因此,只有您的应用程序会监听,用户将无法获得意向选择器。

Try to add this data intent: 尝试添加此数据意图:

<data android:scheme="mycoolapp" android:host="launch" />

With the code above this link should work: 使用上面的代码,此链接应该工作:

<a href="mycoolapp://launch">start my app</a>

I needed a small change to abhishek89m'a answer to make this work. 我需要对abhishek89m的一个小改动做出这个工作的答案。

<a href="intent:#Intent;action=your.example.namespace.CUSTOMACTION;package=your.example.namespace;component=your.example.namespace/.YourActivity;S.extraValueName=WOW;end">
    Launch App
</a>

I removed ".activity" after the slash in component name. 我在组件名称中的斜杠后删除了“.activity”。

And I want to add, that custom action is probably the best answer to this problem if you don't want the app chooser to show up. 我想补充一点,如果您不希望应用选择器显示,那么自定义操作可能是解决此问题的最佳方法。

ps I would add this as comment, but I'm a new user and I don't have enough reputation points. ps我会添加这个作为评论,但我是一个新用户,我没有足够的声誉点。

<a href="your.app.scheme://other/parameters/here">

This link on your browser will launch the app with the specific schema like that on your intent 浏览器上的此链接将启动具有特定模式的应用程序,就像您的意图一样

<intent-filter>
<data android:scheme="your.app.scheme" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>

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

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