multi-device/intents.html

64 строки
3.9 KiB
HTML

<head>
<title>Android Intents with Chrome</title>
</head>
<body>
<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>&lt;iframe src="paulsawesomeapp://page1"&gt;&nbsp;&lt;/iframe&gt;</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 />
&nbsp;&nbsp;&nbsp;HOST/URI-path // Optional host <br />
&nbsp;&nbsp;&nbsp;#Intent; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;package=[string]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action=[string]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;category=[string]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;component=[string]; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scheme=[string]; <br />
&nbsp;&nbsp;&nbsp;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 />
&nbsp;&nbsp;&nbsp;//scan/<br />
&nbsp;&nbsp;&nbsp;#Intent; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;package=com.google.zxing.client.android; <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scheme=zxing; <br />
&nbsp;&nbsp;&nbsp;end; <br />
</p>
<p>To launch the Zxing barcode scanner app, you encode your <code>href</code> on the anchor as follows:</p>
<pre>
&lt;a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"&gt; Take a QR code &lt;/a&gt;
</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>
</body>