Intent在英語中是意圖的意思,在Android中,它是一個將要執行的動作的抽象的描述,一般來說是作爲參數來使用,由Intent來協助完成android各個組件之間的通訊。Intent的用法很多,經過自己的實踐和閱讀網上的一些關於Intent的總結,現將其用法總結如下:
1、調用WEB瀏覽器
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW ,uri);
startActivity(intent);
2、在地圖上顯示某個座標
Uri uri = Uri.parse("geo:44.899533,-77.036476");
Intent intent = new Intent(Intent.Action_VIEW, uri);
startActivity(intent);
3、顯示路徑
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
4、調用撥打電話界面
Uri uri = Uri.parse("tel:10086");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
5、直接撥打電話
需要添加 <uses-permission id="android.permission.CALL_PHONE" /> 這個權限到androidmanifest.xml
Uri uri = Uri.parse("tel.10086");
Intent intent =new Intent(Intent.ACTION_CALL, uri);
6、發送短信或彩信
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra("sms_body", "The SMS text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
7、發送短信
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "test");
startActivity(intent);
8、發送彩信
Uri uri = Uri.parse("content://media/external/images/media/23");
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "some text");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/png");
startActivity(intent);
或者
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
intent.putExtra("subject", "彩信主題");
intent.putExtra("sms_body", "彩信內容");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/images/11.jpg"));
intent.setType("image/jpeg");
startActivity(intent);
9、調用發送郵件功能
Uri uri = Uri.parse("mailto:[email protected]");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
10、發送郵件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, [email protected]);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
或者Intent intent = new Intent(Intent.ACTION_SEND);
String[] tos = {"[email protected]"};
String[] ccs = {"[email protected]"};
intent.putExtra(Intent.EXTRA_EMAIL, tos);
intent.putExtra(Intent.EXTRA_CC, ccs);
intent.putExtra(Intent.EXTRA_TEXT, "The email body text");
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
11、播放媒體文件
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("file:///sdcard/cwj.mp3");
intent.setDataAndType(uri, "audio/mp3");
startActivity(intent);
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
12、卸載APK
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
startActivity(intent);
或者Uri uninstallUri = Uri.fromParts("package", "xxx", null);
Intent intent = new Intent(Intent.ACTION_DELETE, uninstallUri);
13、安裝APK
Uri installUri = Uri.fromParts("package", "xxx", null);
Intent intent = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
14、播放音樂
Uri playUri = Uri.parse("file:///sdcard/download/sth.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, playUri);
15、發送附件
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "The email subject text");
intent.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/test.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(intent, "Choose Email Client"));
16、market上某個應用信,pkg_name就是應用的packageName
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
17、market上某個應用信息,app_id可以通過www網站看下
Uri uri = Uri.parse("market://details?id=app_id");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
18、調用搜索功能
Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "android");
startActivity(intent);
THE END!