Activity跳转
显示跳转和隐式跳转的使用场景:
1,显示意图:启动同一个应用中的Activity
2,隐式意图:启动不同应用中的Activity
在启动效率上,隐式远远低于显示。
如果系统中有多个Activity与意图设置的Action匹配,那么在启动Activity时,会弹出一个对话框,里面包含所有匹配的Activity.
设置intent时如果不去匹配category,系统自动匹配默认的category
1 /** 2 * 跳转至打电话activity 3 * 跳转至其他应用的activity 4 * 隐式跳转:通过指定action和data 5 */ 6 public void click1(View v){ 7 Intent intent = new Intent(); 8 //隐式意图 9 intent.setAction(Intent.ACTION_CALL); 10 intent.setData(Uri.parse("tel:110")); 11 //跳转 12 startActivity(intent); 13 } 14 15 /** 16 * 显示跳转至拨号器 17 * 在本应用中跳转 18 * 显示跳转:直接指定目标Activity的包名和类名 19 */ 20 public void click3(View v){ 21 Intent intent = new Intent(); 22 //指定目标Activity的包名和类名 23 intent.setClassName("com.android.dialer", "com.android.dialer.DialtactsActivity"); 24 startActivity(intent); 25 } 26 27 28 /** 29 * 隐式跳转至拨号器 30 */ 31 public void click4(View v){ 32 Intent intent = new Intent(); 33 //隐式设置拨号器的动作 34 intent.setAction(Intent.ACTION_DIAL); 35 startActivity(intent); 36 } 37 38 /** 39 * 隐式跳转至secondActivity 40 * @param v 41 */ 42 public void click5(View v){ 43 Intent intent = new Intent(); 44 intent.setAction("com.itheima.sa2"); 45 //intent.setData(Uri.parse("heima2:qwe")); 46 //intent.setType("text/username"); 47 //intent.setData(Uri.parse("heima2:qwe123")); 48 49 intent.setDataAndType(Uri.parse("heima2:qwe123"), "text/username"); 50 //系统会自动添加默认的category 51 intent.addCategory(Intent.CATEGORY_DEFAULT); 52 startActivity(intent); 53 } 54 55 /** 56 * 显式跳转至浏览器 57 */ 58 public void click6(View v){ 59 Intent intent = new Intent(); 60 intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); 61 startActivity(intent); 62 } 63 /** 64 * 隐式跳转至浏览器 65 * @param v 66 */ 67 public void click7(View v){ 68 Intent intent = new Intent(); 69 intent.setAction(Intent.ACTION_VIEW); 70 intent.setData(Uri.parse("http://www.baidu.com")); 71 startActivity(intent); 72 }
1 //androidManifest.xml清单文件 2 3 <activity 4 android:name="com.itheima.jump.MainActivity" 5 android:label="@string/app_name" > 6 <intent-filter> 7 <action android:name="android.intent.action.MAIN" /> 8 9 <category android:name="android.intent.category.LAUNCHER" /> 10 </intent-filter> 11 </activity> 12 <activity android:name=".SecondActivity"> 13 <intent-filter > 14 <action android:name="com.itheima.sa"/> 15 <action android:name="com.itheima.sa3"/> 16 <data android:scheme="heima"/> 17 <data android:scheme="heima3"/> 18 <category android:name="android.intent.category.DEFAULT"/> 19 </intent-filter> 20 21 <intent-filter > 22 <action android:name="com.itheima.sa2"/> 23 <data android:scheme="heima2" android:mimeType="text/username"/> 24 <category android:name="android.intent.category.DEFAULT"/> 25 </intent-filter> 26 </activity>