2013年9月4日 星期三

Android App設計 - 從網頁開啟App

首先為App之AndroidManifest.xml加入一個App專屬的<intent-filter>,例如
    <activity
        android:name="MyappMain"
        android:label="@string/app_name"
        android:noHistory="true" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <data android:scheme="nkut.edu.sswuApp" android:host="nkut" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT"/>
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>

在此設定的android:scheme屬性值就是HTML網頁中連結App的通關密語,只要在網頁中使用超連結到"nkut.edu.sswuApp"即可,例如:

<html>
    <head>
        <title>launch apk from browser</title>
        <meta http-equiv = "Content-Type" content = "text/html; charset=utf-8">
    </head>
    <body>
        <p>
        <a href = "intent://nkut#Intent;scheme=nkut.edu.sswuApp;end">點我開啟APP</a></p>
    </body>
</html>