- Intent intent = new Intent(this, xxx.class);
- startActivity(intent);
即可
- action
- Intent intent = new Intent("this is an action");
- startActivity(intent);
中进行配置(浏览网页的时候点击一些广告可以进对应的app大概就是这个原理)
- AndroidManifest
- <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>
属性,那要打开这个Activity也是so easy的
- android:scheme
- startActivity(new Intent(Intent.ACTION_VIEW,
- Uri.parse("azhon.scheme://")))
- ;
- <data android:scheme="string"
- android:host="string"
- android:port="string"
- android:path="string"
- android:pathPattern="string"
- android:pathPrefix="string"
- android:mimeType="string" />
- <scheme>
- ://
- <host>
- :
- <port>
- [
- <path>
- |
- <pathPrefix>
- |
- <pathPattern>
- ]
scheme开头 :// 主机名 : 端口号 / [路径/参数] 路径后面拼接参数也是可以的下面会用到。
这个地址和我们经常使用的网页地址差不多是一样滴
eg:https://loaclhost:8080/index.jsp
- <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>
- <WebView
- android:id="@+id/web"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp" />
- <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>
效果图:
在网页中只需要使用一个超链接便可以轻松的跳转至我们的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()));
- <a href="weixin://">打开微信</a>
- <p>
- <a href="mqqzone://">打开QQ</a>
效果图:
来源: https://juejin.im/entry/59f304b3f265da430b7a7d24