简体   繁体   中英

How to open an Android application directly from your own HTML page

我想公开记录该知识,以便其他人(包括我自己)可以在以后找到它。

If you use your own web page in WebView then this can be the solution:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webView1);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
        webView.loadUrl("file:///android_asset/index.html");

    }

    public class WebAppInterface {
        Context mContext;

        public WebAppInterface(Context c) {
            mContext = c;
        }

        @JavascriptInterface
        public void openMyApp() {
            PackageManager pManager = getPackageManager();
            // here you should use a package name of the application
            // you want to open
            Intent launchIntent = pManager.getLaunchIntentForPackage("com.android.mms");
            startActivity(launchIntent);
        }

    }

}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.webviewtest.MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

assets/index.html

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body style="text-align: center;">

<script type="text/javascript">
    function openApp() {
        Android.openMyApp();
    }
</script>

<h1>Open app test</h1>
<button type="button" onclick="openApp()">Click Me!</button>

</body>

Using Android standards

You should use a custom link and intent-filter for this purpose:

make a link with href set as

android-app://com.example.android/http/example.com/gizmos?1234

in your app's manifest file declare an intent-filter as

<activity
        android:name=".MainActivity"
        android:label="MyApp"
        android:screenOrientation="portrait" >
        <intent-filter>
            <data android:scheme="android-app"     
                  android:host="com.example.android" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
</activity>

in your Main activity handle the incoming intent as :

//required only if you want to get some params from url
Intent intent = getIntent();
Uri data = intent.getData();

This would open MainActivity of the app if its installed on the device or take the user to example.com otherwise.

References:

https://developer.android.com/training/app-indexing/deep-linking.html https://developers.google.com/app-indexing/webmasters/server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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