天天看點

【android基礎】Android Intent 用法全面總結

[代碼] 調用撥号程式

   // 給移動客服10086撥打電話

    Uri uri = Uri.parse("tel:10086");

    Intent intent = new Intent(Intent.ACTION_DIAL, uri);

    startActivity(intent);

[代碼] 發送短信或彩信

    // 給10086發送内容為“Hello”的短信

    Uri uri = Uri.parse("smsto:10086");

    Intent intent = new Intent(Intent.ACTION_SENDTO, uri);

    intent.putExtra("sms_body", "Hello");

    // 發送彩信(相當于發送帶附件的短信)

    Intent intent = new Intent(Intent.ACTION_SEND);

    Uri uri = Uri.parse("content://media/external/images/media/23");

   intent.putExtra(Intent.EXTRA_STREAM, uri);

    intent.setType("image/png");

[代碼] 通過浏覽器打開網頁

    // 打開Google首頁

    Uri uri = Uri.parse("http://www.google.com");

    Intent intent  = new Intent(Intent.ACTION_VIEW, uri);

[代碼] 發送電子郵件

    // 給[email protected]發郵件

    Uri uri = Uri.parse("mailto:[email protected]");

    // 給[email protected]發郵件發送内容為“Hello”的郵件

    intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");

    intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");

    intent.putExtra(Intent.EXTRA_TEXT, "Hello");

    intent.setType("text/plain");

    // 給多人發郵件

    Intent intent=new Intent(Intent.ACTION_SEND);

   String[] tos = {"[email protected]", "[email protected]"}; // 收件人

    String[] ccs = {"[email protected]", "[email protected]"}; // 抄送

    String[] bccs = {"[email protected]", "[email protected]"}; // 密送

    intent.putExtra(Intent.EXTRA_EMAIL, tos);

    intent.putExtra(Intent.EXTRA_CC, ccs);

    intent.putExtra(Intent.EXTRA_BCC, bccs);

   intent.putExtra(Intent.EXTRA_TEXT, "Hello");

    intent.setType("message/rfc822");

   startActivity(intent);

[代碼] 顯示地圖與路徑規劃

   // 打開Google地圖中國北京位置(北緯39.9,東經116.3)

    Uri uri = Uri.parse("geo:39.9,116.3");

    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

    // 路徑規劃:從北京某地(北緯39.9,東經116.3)到上海某地(北緯31.2,東經121.4)

    Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");

[代碼] 播放多媒體

    Intent intent = new Intent(Intent.ACTION_VIEW);

   Uri uri = Uri.parse("file:///sdcard/foo.mp3");

    intent.setDataAndType(uri, "audio/mp3");

    Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");

[代碼] 拍照

    // 打開拍照程式

   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(intent, 0);

    // 取出照片資料

    Bundle extras = intent.getExtras();

   Bitmap bitmap = (Bitmap) extras.get("data");

[代碼] 擷取并剪切圖檔

   // 擷取并剪切圖檔

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

    intent.setType("image/*");

    intent.putExtra("crop", "true"); // 開啟剪切

   intent.putExtra("aspectX", 1); // 剪切的寬高比為1:2

    intent.putExtra("aspectY", 2);

    intent.putExtra("outputX", 20); // 儲存圖檔的寬和高

    intent.putExtra("outputY", 40);

   intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 儲存路徑

    intent.putExtra("outputFormat", "JPEG");// 傳回格式

    // 剪切特定圖檔

    Intent intent = new Intent("com.android.camera.action.CROP");

    intent.setClassName("com.android.camera", "com.android.camera.CropImage");

    intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));

    intent.putExtra("outputX", 1); // 剪切的寬高比為1:2

   intent.putExtra("outputY", 2);

    intent.putExtra("aspectX", 20); // 儲存圖檔的寬和高

  intent.putExtra("aspectY", 40);

    intent.putExtra("scale", true);

    intent.putExtra("noFaceDetection", true);

   intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));

[代碼] 打開Google Market

    // 打開Google Market直接進入該程式的詳細頁面

    Uri uri = Uri.parse("market://details?id=" + "com.demo.app");

   Intent intent = new Intent(Intent.ACTION_VIEW, uri);

[代碼] 安裝和解除安裝程式

    Uri uri = Uri.fromParts("package", "com.demo.app", null);

    Intent intent = new Intent(Intent.ACTION_DELETE, uri);

[代碼] 進入設定界面

   // 進入無線網絡設定界面(其它可以舉一反三)

  Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);

   startActivityForResult(intent, 0);

繼續閱讀