跳轉(zhuǎn)方式
a、顯示跳轉(zhuǎn)
-
顯示方式一
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、隱示跳轉(zhuǎn)
Action跳轉(zhuǎn)
-
界面代碼
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跳轉(zhuǎn)
-
介紹
URL Scheme協(xié)議格式:scheme://host:port/path 模式://主機(jī):端口/路徑
-
樣例
myapp://user:8080/userinfo?userId=1234
-
參數(shù)
myapp 指Scheme 的名字 user 指Scheme 的地址 8080 指端口號 userinfo 指跳轉(zhuǎn)的頁面 userid 指傳遞的參數(shù)
-
Activity跳轉(zhuǎn)代碼
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("myapp://user:8080/userinfo?userid=1234")); startActivity(intent);
-
網(wǎng)頁跳轉(zhuǎn)(使用系統(tǒng)自帶瀏覽器或者谷歌瀏覽器)
<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(); //獲取指定參數(shù)值 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); }