android:网页使用网址访问activity

这篇博客介绍了如何在Android中通过自定义URL scheme来启动Activity。文章提供了一个简单的Demo,展示了如何配置URL并从网页中触发Activity的启动。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网页打开activity有好几种方法,不过现在我主要介绍的是使用网页打开activity。

首先我们都知道的是,网址的格式:content://host:port/path  ,比如:http://10.205.1.10:8080/text.html .

在android中我们也可以使用scheme给activity设置这些东西,设置的方法为:

<activity
            android:name="你的Activity地址"
            android:exported="true"
            android:label="@string/title_activity_intent" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="10.205.1.10" //随你喜欢设置成什么,可以是任何类型的数据
                    android:scheme="content" />//和host一样
            </intent-filter>
        </activity>
代表scheme的网址格式是:

content://com.example.project:8080/folder/subfolder/etc 
\---------/  \---------------------------/ \---/ \--------------------------/ 
scheme                 host        port        path 
                \--------------------------------/ 
                          authority    
我们就可以直接获取Activity了,

下面是一个demo:

首先是网页:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<a href="content://thisisatest:8080?test1=1&test2=2" >test</a>  
</body>
</html>


然后我们的Activity设置:

 <intent-filter>
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />

                <data
                    android:host="thisisatest"
                    android:port="8080"
                    android:scheme="content" />
            </intent-filter>
这样我们就可以跳转了

获取数据的话 我们需要添加一下代码:

<span style="white-space:pre">		</span>Intent intent = getIntent();
		String scheme = intent.getScheme();
		Uri uri = intent.getData();
<span style="white-space:pre">		</span>String id = uri.getQueryParameter("test1");
<span style="white-space:pre">		</span>String returnurl = uri.getQueryParameter("test2") ;






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值