跳转方式
a、显示跳转
-
显示方式一
Intent intent=new Intent(context,SecondeActivity.class); startActivity(intent);
-
显示方式二
Intent intent=new Intent(); intent.setClass(context,SecondeActivity.class); startActivity(intent);
-
显示方式三
Intent intent=new Intent(); intent.setComponent(new ComponentName(context, SecondeActivity.class)); startActivity(intent);
b、隐示跳转
Action跳转
-
界面代码
Intent intent = new Intent(); intent.setAction("com.test.seconde"); startActivity(intent);
-
AndroidManifest代码
<activity android:name=".activity.startway.BActivity"> <intent-filter> <action android:name="com.test.seconde" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Scheme跳转
-
介绍
URL Scheme协议格式:scheme://host:port/path 模式://主机:端口/路径
-
样例
myapp://user:8080/userinfo?userId=1234
-
参数
myapp 指Scheme 的名字 user 指Scheme 的地址 8080 指端口号 userinfo 指跳转的页面 userid 指传递的参数
-
Activity跳转代码
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://user:8080/userinfo?userid=1234")); startActivity(intent);
-
网页跳转(使用系统自带浏览器或者谷歌浏览器)
<a href="myapp://user:8080/userinfo?userid=1234">用户详情</a>
-
AndroidManifest代码
<activity android:name=".activity.startway.BActivity"> <intent-filter> <data android:host="user" android:path="/userinfo" android:port="8080" android:scheme="myapp" /> <category android:name="android.intent.category.DEFAULT" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> </activity>
-
Activity接收代码
Uri uri = getIntent().getData(); String url = uri.toString(); // scheme值 String scheme = uri.getScheme(); // host值 String host = uri.getHost(); //port值 int port = uri.getPort(); // 访问路劲 String path = uri.getPath(); List<String> pathSegments = uri.getPathSegments(); // Query值 String query = uri.getQuery(); //获取指定参数值 String userid = uri.getQueryParameter("userid");
-
额外判断
PackageManager packageManager = getPackageManager(); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://user:8080/userinfo?userid=1234")); List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0); boolean isHas = !activities.isEmpty(); if (isHas) { startActivity(intent); }
网友评论