解锁Activity的跳转新姿势———使用scheme跳转
2018-02-05 23:50
357 查看
Activity的跳转可以说是非常简单的了、从一个页面跳转至另一个页面,我们最常用的也就是下面这种:
Intent intent = new Intent(this, xxx.class); startActivity(intent);1
2
[/code]
当然还有我们的隐式跳转,为Intent指定一个action
即可
Intent intent = new Intent("this is an action"); startActivity(intent);1
2
[/code]
这篇文章上面提到的两个都不讲,这里来说使用scheme协议来进行页面跳转
manifest / data 配置如果需要让我们的Activity能被其他应用或者网页所打开,需要在
AndroidManifest中进行配置(浏览网页的时候点击一些广告可以进对应的app大概就是这个原理)
<activity android:name=".TestOneActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!--配置scheme--> <data android:scheme="azhon.scheme" /> </intent-filter> </activity>1
2
3
4
5
6
7
8
9
10
[/code]
如上代码我们只是简单的配置了一个
android:scheme属性,那要打开这个Activity也是so easy的
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("azhon.scheme://")));1
2
[/code]
当然< data/>中还可以配置更多的属性(截取自官方中文文档,可以点击上面的manifest / data 配置)
<data android:scheme="string" android:host="string" android:port="string" android:path="string" android:pathPattern="string" android:pathPrefix="string" android:mimeType="string" />1
2
3
4
5
6
7
[/code]
scheme 路径的规则
<scheme> :// <host> : <port> [<path>|<pathPrefix>|<pathPattern>]1
[/code]
scheme开头 :// 主机名 : 端口号 / [路径/参数] 路径后面拼接参数也是可以的下面会用到。
这个地址和我们经常使用的网页地址差不多是一样滴
eg:https://loaclhost:8080/index.jsp
在网页中打开我们对应的页面
我们修改Manifest中Activity的配置在多加点料,我这里创建了两个页面<activity android:name=".TestOneActivity"> <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="azhon" android:port="1011" android:scheme="azhon.scheme" /> </intent-filter> </activity> <activity android:name=".TestTwoActivity"> <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="azhon" android:port="1012" android:scheme="azhon.scheme" /> </intent-filter> </activity>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[/code]
在主页面布局添加一个WebView控件,并让它加载
assets文件夹中的html文件
布局文件代码:
<WebView android:id="@+id/web" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" />1
2
3
4
5
[/code]
assets文件夹中的Html代码:
<a href="azhon.scheme://azhon:1011/我是路径?user=958460248&psd=123456">跳转至TestOneActivity</a> <p> <a href="azhon.scheme://azhon:1012/i am path?toId=25&tmId=888">跳转至TestTwoActivity</a>1
2
3
[/code]
效果图:
在网页中只需要使用一个超链接便可以轻松的跳转至我们的app,是不是很开心。
当你将这个html文件使用手机的浏览器查看时,点击也是一样可以跳转进app的
跳转页面的时候还可以通过 ?key=value&key1=value2 键值对的方式往地址后面拼接参数
在跳转过去的页面接收传递过来的参数
Intent intent = getIntent(); Uri uri = intent.getData(); assert uri != null; sb.append("scheme: ").append(intent.getScheme()).append("<p>"); sb.append("host: ").append(uri.getHost()).append("<p>"); sb.append("port: ").append(uri.getPort()).append("<p>"); sb.append("path: ").append(uri.getPath()).append("<p>"); sb.append("params: ").append(uri.getQuery()).append("<p>"); //获取跳转过来携带所有参数的 键名 Set<String> names = uri.getQueryParameterNames(); Iterator<String> iterator = names.iterator(); while (iterator.hasNext()) { String key = iterator.next(); sb.append(key).append(": ").append(uri.getQueryParameter(key)).append("<p>"); } tv.setText(Html.fromHtml(sb.toString()));1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[/code]
有了这种跳转方式 你只要知道其他app的scheme你就可以随意的跳过去了想想都刺激,比如下面代码打开微信和QQ程序。
<a href="weixin://">打开微信</a> <p> <a href="mqqzone://">打开QQ</a>1
2
3
[/code]
效果图:
这里提供了Demo下载
小技巧:查看其他app在Manifest中声明的scheme地址,这里分享一个教程
如果你使用的是Android Studio2.4或者更高版本你可以直接将apk拖进Android Studio即可查看。查看APK中AndroidManifest.xml内容
转自:http://blog.csdn.net/a_zhon/article/details/78358310
相关文章推荐
- 解锁Activity的跳转新姿势———使用scheme跳转
- android开发(41) Fragment中使用POP_BACK_STACK_INCLUSIVE达到一次跳转到栈底。类似Activity的 采用FLAG_ACTIVITY_CLEAR_TOP
- Android学习之使用startActivityForResult()和onActivityResult()进行跳转并返回数据
- android 使用多个Intent 进行activity跳转 而没有finish的情况,如何直接退出系统
- Android使用intent跳转到其它应用activity界面
- Android 使用Intent实现Activity跳转和数据传递
- androidSpinner使用及根据选择项跳转到相应的Activity
- Kotlin android简单Activity跳转、handler和thread简单配合使用
- Activity中使用Intent实现页面跳转与参数的传递
- Activity之间的跳转及销毁所有Activity的正确姿势
- 使用Intent对象来实现两个(或多个)Activity之间的跳转,并实现在Activity之间传递参数。
- 【Android】使用Intent实现Activity之间的跳转(创建一个Activity的过程)
- Android知识点2-Adapter中使用Intent跳转到另一个activity
- Activity中使用Intent实现页面跳转与参数的传递(转)
- 使用 Bundle 在 Activity 间传递数据 activity1跳转到activity2中,然后activity2中返回值给activity1中 实例
- 如何跳转回已打开的Activity并传参(不使用startActivityForResult)
- 解锁glide 4.0使用 新姿势
- 【求助】使用intent-filter跳转activity出错
- 点击多个URL跳转多个不同界面使用一个Activity实现
- ActivityLifecycleCallbacks的正确使用姿势