网页启动本地Activity

848 阅读1分钟

前言

Intent 这个类在开发中是很常用的类,代表了着一个意图(获取理解为目标、目的),首先我们需要明确一点的就是:任何一个浏览器链接都是一个隐式意图,打开一个浏览器的方式无非就是显式意图和隐式意图,所以我们配置过滤器即可!

示例

首先,工程目录如图:

MainActivity 和布局文件都不用改,关键是 manifests 文件中 LocalAppAty 的配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xpu.launchlocalapp">
 
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:>
            <intent-filter>
                <action android: />
 
                <category android: />
            </intent-filter>
        </activity>
        <activity android:>
            <intent-filter>
                <!--可以被浏览器启动的Activity-->
                <category android:></category>
                <category android:></category>
 
                <action android:></action>
                <data android:scheme="app" ></data>
            </intent-filter>
        </activity>
    </application>
</manifest>

JavaWeb 工程如下,一个很简单的标签:

一般安卓模拟器要访问本机的 ip 地址,使用 10.0.0.2, 端口号还是与你的服务器一致,我的是 8080:

成功开启:

同时获取到了启动该 Activity 的信息来源: