在Android平台URI主要分五个部分:scheme, authority, path, queryParameter, queryString。其中authority又分为host和port。格式如下:(url的所有字母命名只能为小写) scheme://host:port/path?qureyParameter=queryString 例如:myscheme://www.febmaple.com:80/mypath?key=mykey
在Android的Manifest配置文件中,在要启动的activity下配置项中有配置。 其中包含内容有:
<data android:host="" android:mimeType="" android:path="" android:pathPattern="" android:pathPrefix="" android:port="" android:scheme="" android:ssp="" android:sspPattern="" android:sspPrefix=""/>1、web端html里写入 <a href="myscheme://www.febmaple.com:80/mypath?key=mykey">Click</a> 2、android端在menifest的响应activity的intentfilter下配置,一般只需配置scheme和host即可。 3、这样手机系统自带浏览器碰到不能处理的scheme之后会发送intent给能处理的应用,因为我们的app可以处理该scheme,所以我的app得到启动。(ps:如果用webview加载html,webview碰到处理不了的scheme并不会发送intent找app处理,而系统自带浏览器是可以的,当然我们的需求就是用系统自带浏览器触发)。
Show u my code:
一、跳到app首页:新建一个app工程用webview加载所写的html,用以触发目标app。(实际需求是直接在系统浏览器里触发目标app) 1、建立html放到工程的main/assets目录下
<!DOCTYPE html> <html> <body> <h1>Test Scheme</h1> <!--手动点击跳转--> <a href="myscheme://www.febmaple.com:80/mypath?key=mykey">Click</a> </body> </html>2、webview加载本地html内容来触发目标app
wvUrl.loadUrl("file:///android_asset/test.html");3、在app manifeset的**欢迎**activity添加intent-filter配置data标签.
<activity android:name=".ui.launching.DemoLaunchingActivity" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> <!--网页跳转intentfilter--> <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="myscheme"/> </intent-filter> </activity>4、在配置好的Activity里即可获取外部跳转的参数信息。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main_launching); //接收网页的跳转 Intent intent = getIntent(); //Log.e("febmaple", "scheme:" +intent.getScheme()); if (intent != null) { Uri uri = intent.getData();//获取web端传入的data数据 if (uri != null) { Log.e("febmaple", "scheme: " + uri.getScheme()); Log.e("febmaple", "host: " + uri.getHost()); Log.e("febmaple", "port: " + uri.getPort()); Log.e("febmaple", "path: " + uri.getPath()); //Log.e("febmaple", "queryString: "+uri.getQuery()); Log.e("febmaple", "queryParameter: " + uri.getQueryParameter("key")); } } }二、如果要跳特定界面: 先根据scheme跳到欢迎界面或者主界面,然后再根据queryparameter的参数来确定跳到哪,这样避免了对每个activity都要设置intentfilter的data标签。(如果非要给多个页面配intentfilter,那直接用path作为页面区分的标志就好了)
下面是在欢迎界面获取到web端数据后再跳到特定activity的code: 1、在html中把queryparameter设为要跳的activity的名字
<a href="myscheme://www.febmaple.com:80/mypath?key=com.xxxxx.app.ui.setting.DemoActivity">Click</a>2、在欢迎页activity里启动目标activity。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main_launching); //接收网页的跳转 Intent intent = getIntent(); if (intent != null) { Uri uri = intent.getData();//获取web端传入的data数据 if (uri != null) { aimActivityName = uri.getQueryParameter("key"); Intent intent1 = new Intent(); intent1.setClassName("com.xxxx.app", aimActivityName); startActivity(intent1); } } }把路由activity设为不可见
<activity android:name=".ui.web.SchemeFilterActivity" android:launchMode="singleInstance" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoDisplay"> <!--网页跳转intentfilter,path用于确定web跳哪个页面--> <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="www.febmaple.com" android:scheme="myscheme" /> </intent-filter> </activity>ps:自动跳转的html:
<!DOCTYPE html> <html> <body> <h1>Test Scheme</h1> <!--自动加载隐藏页面跳转--> <iframe src="myscheme://www.febmaple.com:80/mypath?key=mykey" style="display:none"></iframe> <!--手动点击跳转--> <a href="myscheme://www.febmaple.com:80/mypath?key=mykey">Click</a> </body> </html>