Android 通过网页打开自己的APP(scheme)

通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。

首先在Mainifest文件里面对要启动的Activity添加一个过滤器。

<activity
            android:name="com.example.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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:scheme="znn"/>
            </intent-filter>
        </activity>

在MainActivity接收数据:

public class MainActivity extends Activity implements View.OnClickListener{

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

		Intent intent = getIntent();
		String scheme = intent.getScheme();
		Uri uri = intent.getData();
		System.out.println("scheme:"+scheme);
		if (uri != null) {
			String host = uri.getHost();
			String dataString = intent.getDataString();
			String id = uri.getQueryParameter("d");
			String path = uri.getPath();
			String path1 = uri.getEncodedPath();
			String queryString = uri.getQuery();
			System.out.println("host:"+host);
			System.out.println("dataString:"+dataString);
			System.out.println("id:"+id);
			System.out.println("path:"+path);
			System.out.println("path1:"+path1);
			System.out.println("queryString:"+queryString);
		}
		
	}
}


写一个最简单的网页:

<a href="znn://aa.bb:80/test?p=12&d=1">test</a>



测试地址: www.erdian.net/m.html

测试结果:

scheme:znn
host:aa.bb
dataString:znn://aa.bb:80/test?p=12&id=1
id:1
path:/test
path1:/test

queryString:p=12&d=1


http://www.erdian.net/?p=238

  • 17
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
Android app 使用scheme可以让其他应用程序或者网页链接打开你的应用程序,并且可以携带参数,实现应用程序之间的跳转和交互。以下是Android app 使用scheme的步骤: 1. 在AndroidManifest.xml文件中注册scheme。例如,你可以在<activity>标签中添加一个<intent-filter>标签,并且在<intent-filter>标签中添加一个<data>标签,如下所示: ```xml <activity android:name=".MainActivity"> <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:scheme="myapp" android:host="openactivity" /> </intent-filter> </activity> ``` 2. 在应用程序中处理scheme。在你的MainActivity中,你可以通过以下代码来处理scheme: ```java @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = getIntent(); String action = intent.getAction(); Uri data = intent.getData(); if (data != null) { String host = data.getHost(); if (host.equals("openactivity")) { //处理scheme参数,打开指定的Activity } } } ``` 3. 在其他应用程序或者网页中使用scheme打开你的应用程序。例如,你可以在浏览器中输入“myapp://openactivity”来打开你的应用程序并且跳转到指定的Activity。 注意事项: 1. 必须在<intent-filter>标签中添加<category android:name="android.intent.category.BROWSABLE" />,否则无法在浏览器中打开应用程序。 2. scheme参数区分大小写。 3. 如果你的应用程序中有多个Activity需要处理scheme,需要在<intent-filter>标签中分别添加<data>标签,每个<data>标签的scheme和host必须不同。
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值