59 строки
3.8 KiB
HTML
59 строки
3.8 KiB
HTML
<h1>Android Intents with Chrome</h1>
|
|
|
|
<p>A little known feature in Android lets you launch apps directly from a web page via an <a href="http://developer.android.com/guide/components/intents-filters.html">Android Intent</a>. One scenario is launching an app when the user lands on a page, which you can achieve by embedding an iframe in the page with a custom URI-scheme set as the <code>src</code>, as follows: <code><iframe src="paulsawesomeapp://page1"> </iframe></code>. This works in the Chrome for Android browser, version 18 and earlier. It also works in the Android browser, of course.</p>
|
|
|
|
<p>The functionality has changed slightly in Chrome for Android, versions 25 and later. It is no longer possible to launch an Android app by setting an iframe's <code>src</code> attribute. For example, navigating an iframe to a URI with a custom scheme such as <code>paulsawesomeapp://</code> will not work even if the user has the appropriate app installed. Instead, you should implement a user gesture to launch the app via a custom scheme, or use the “intent:” syntax described in this article.</p>
|
|
|
|
<h2>Syntax</h2>
|
|
|
|
<p>The best practice is to construct an intent anchor and embed that into the page so the user can launch the app. This gives you a lot more flexibility in controlling how apps are launched, including the ability to pass extra information into the app via <a href="http://developer.android.com/guide/components/intents-filters.html#extras">Intent Extras</a>.</p>
|
|
|
|
<p>The basic syntax for an intent-based URI is as follows:</p>
|
|
|
|
<p>intent:<br />
|
|
HOST/URI-path // Optional host <br />
|
|
#Intent; <br />
|
|
package=[string]; <br />
|
|
action=[string]; <br />
|
|
category=[string]; <br />
|
|
component=[string]; <br />
|
|
scheme=[string]; <br />
|
|
end; <br />
|
|
</p>
|
|
|
|
<p>See the <a href="https://code.google.com/p/android-source-browsing/source/browse/core/java/android/content/Intent.java?repo=platform--frameworks--base#6514">Android source</a> for parsing details.</p>
|
|
|
|
<h2>Example</h2>
|
|
|
|
<p>Here's an intent that launches the Zxing barcode scanner app. It follows the syntax thus:</p>
|
|
|
|
<p>intent:<br />
|
|
//scan/<br />
|
|
#Intent; <br />
|
|
package=com.google.zxing.client.android; <br />
|
|
scheme=zxing; <br />
|
|
end; <br />
|
|
</p>
|
|
|
|
<p>To launch the Zxing barcode scanner app, you encode your <code>href</code> on the anchor as follows:</p>
|
|
|
|
<pre>
|
|
<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"> Take a QR code </a>
|
|
</pre>
|
|
|
|
<p>See the <a href="https://code.google.com/p/zxing/source/browse/trunk/android/AndroidManifest.xml#97">Android Zxing Manifest</a>, which defines the package and the host.</p>
|
|
|
|
<h2>Considerations</h2>
|
|
|
|
<p>If the activity you invoke via an intent contains <a href="http://developer.android.com/guide/components/intents-filters.html#extras">extras</a>, you can include these as well.</p>
|
|
|
|
<p>Only activities that have the category filter, <a href="http://developer.android.com/reference/android/content/Intent.html#CATEGORY_BROWSABLE">android.intent.category.BROWSABLE</a> are able to be invoked using this method as it indicates that the application is safe to open from the Browser.</p>
|
|
|
|
<h2>See also</h2>
|
|
|
|
<ul>
|
|
<li><a href="http://developer.android.com/guide/components/intents-filters.html">Android Intents and Intent Filters</a></li>
|
|
<li><a href="http://developer.android.com/guide/components/activities.html">Android Activities</a></li>
|
|
</ul>
|
|
|