顯式跳轉
是在已知包名和類名的情況下常用的跳轉方法:
- Intent mIntent = new Intent();
- mIntent.setClassName("com.android.settings","com.android.settings.Settings");
- mContext.startActivity(mIntent);
我們也常這么用:
- Intent intent = new Intent(mContext, XXActivity.class);
- startActivity(intent);
這是跳轉到當前應用的某個Activity,相信大家都十分熟悉,今天主要講的是如何使用隱式intent意圖跳轉
隱式跳轉
1、隱式跳轉之Action跳轉
假定有一個Activity在清單中是這樣的聲明的:
- <activity android:name=".ActionActivity";
- <intent-filter
- action android:name="customer_action_here"
- </intent-filter>
- </activity>
- //創建一個隱式的 Intent 對象:Action 動作
- Intent intent = new Intent();
- //設置 Intent 的動作為清單中指定的action
- intent.setAction("customer_action_here");
- startActivity(intent);
2、隱式跳轉之Category跳轉
假定有一個Activity在清單中是這樣聲明的:
- <activity android:name=".CategoryActivity" >
- <intent-filter>
- <action android:name="customer_action_here" />
- <category android:name="customer_category_here" />
- </intent-filter>
- </activity>
我們可以使用如下代碼進行跳轉到以上Activity:
- //創建一個隱式的 Intent 對象:Category 類別
- Intent intent = new Intent();
- intent.setAction("customer_action_here");
- //添加與清單中相同的自定義category
- intent.addCategory("customer_category_here");
- startActivity(intent);
3、隱式跳轉之Data跳轉
假定有一個Activity是這樣定義的:
- < activity android:name=".DataActivity">
- < intent-filter>
- < category android:name="android.intent.category.DEFAULT" />
- < data
- android:scheme="content"
- android:host="com.example.intentdemo"
- android:port="8080"
- android:pathPattern=".*pdf"
- android:mimeType="text/plain"/>
- < /intent-filter>
- < /activity>
我們可以使用如下代碼進行跳轉:
- //創建一個隱式的 Intent 對象,方法四:Date 數據
- Intent intent = new Intent();
- Uri uri = Uri.parse("content://com.example.intentdemo:8080/abc.pdf");
- //注:setData、setDataAndType、setType 這三種方法只能單獨使用,不可共用
- //單獨以 setData 方法設置 URI
- //intent.setData(uri);
- //單獨以 seType 方法設置 Type
- //intent.setType("text/plain");
- //上面分步驟設置是錯誤的,要么以 setDataAndType 方法設置 URI 及 mime type
- intent.setDataAndType(uri, "text/plain");
- startActivity(intent);
4、隱式跳轉之調用系統應用
4.1 使用瀏覽器瀏覽網頁
- //web瀏覽器
- Uri uri= Uri.parse("http://www.baidu.com");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
4.2 調用地圖
- //打開地圖查看經緯度
- Uri uri = Uri.parse("geo:38.899533,-77.036476");
- Intent intent = new Intent(Intent.ACTION_VIEW, uri);
- startActivity(intent);
4.3 調用電話撥號(不需要撥號權限)
- Uri uri = Uri.parse("tel:10086");
- Intent intent = new Intent(Intent.ACTION_DIAL, uri);//注意區別於下面4.4的action
- startActivity(intent);
4.4 調用電話直接撥號(需要撥號權限)
- Uri uri = Uri.parse("tel:15980665805");
- Intent intent = new Intent(Intent.ACTION_CALL, uri);//注意區別於上面4.3的aciton
- startActivity(intent);
4.5 調用短信程序(無需發送短信權限,接收者自填)
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.putExtra("sms_body", "這里寫短信內容");
- intent.setType("vnd.android-dir/mms-sms");
- startActivity(intent);
4.6 調用短信程序(無需發送短信權限)
- Uri uri = Uri.parse("smsto:10086");//指定接收者
- Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
- intent.putExtra("sms_body", "你這個黑心運營商");
- startActivity(intent);
4.7 調用郵件程序
- Intent intent = new Intent(Intent.ACTION_SENDTO);
- intent.setData(Uri.parse("mailto:xxx@gmail.com"));
- intent.putExtra(Intent.EXTRA_SUBJECT, "這是標題");
- intent.putExtra(Intent.EXTRA_TEXT, "這是內容");
- startActivity(intent);
4.8 調用音樂播放器
- Intent intent = new Intent(Intent.ACTION_VIEW);
- //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");
- Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");
- intent.setDataAndType(uri, "audio/mp3");
- startActivity(intent);
4.9 調用視頻播放器
- Intent intent = new Intent(Intent.ACTION_VIEW);
- //Uri uri = Uri.parse("file:///sdcard/xiong_it.mp3");
- Uri uri = Uri.parse("file:///sdcard/xiong_it.mp4");
- intent.setDataAndType(uri, "video/mp4");
- startActivity(intent);
4.10 調用搜索
- Intent intent = new Intent();
- intent.setAction(Intent.ACTION_WEB_SEARCH);
- intent.putExtra(SearchManager.QUERY, "android");
- startActivity(intent);
總結
相信大家經過上面的介紹,已經對Intent跳轉有了一個比較成熟的理解,Intent是組件之間的紐帶,使用它可以讓系統替我們完成很多工作,不需要我們來指定完成工作的程序。實際上,我們會發現,調用系統程序使用液無非是隱式跳轉,只不過使用的是系統內置的一些Action,Uri,Data等信息而已。希望對大家有所幫助。