如何直接从您自己的HTML页面打开Android应用程序

时间:2023-01-25 07:37:56

I would like to document that knowledge in public so that others (including myself) can find it later.

我想在公共场合记录这些知识,以便其他人(包括我自己)可以在以后找到它。

2 个解决方案

#1


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

如果您在WebView中使用自己的网页,那么这可以是解决方案:

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>

#2


Using Android standards

使用Android标准

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

您应该为此目的使用自定义链接和intent-filter:

make a link with href set as

将href设置为的链接

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

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

在你的应用程序的清单文件中声明一个intent-filter为

<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 :

在您的Main活动中处理传入的意图:

//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.

如果应用程序安装在设备上,则会打开应用程序的MainActivity,否则将用户带到example.com。

References:

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

#1


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

如果您在WebView中使用自己的网页,那么这可以是解决方案:

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>

#2


Using Android standards

使用Android标准

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

您应该为此目的使用自定义链接和intent-filter:

make a link with href set as

将href设置为的链接

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

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

在你的应用程序的清单文件中声明一个intent-filter为

<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 :

在您的Main活动中处理传入的意图:

//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.

如果应用程序安装在设备上,则会打开应用程序的MainActivity,否则将用户带到example.com。

References:

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