Android Intent 用法全面總結

來源:互聯網
上載者:User

標籤:

http://www.oschina.net/code/snippet_54100_7587#12077

[1].[代碼] 調用撥號程式 

1

2

3

4

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

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

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

startActivity(intent);

[2].[代碼] 傳送簡訊或多媒體訊息 

1

2

3

4

5

6

7

8

9

10

11

12

// 給10086發送內容為“Hello”的簡訊

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

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

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

startActivity(intent);

// 發送多媒體訊息(相當於發送帶附件的簡訊)

Intent intent = new Intent(Intent.ACTION_SEND);

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

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

intent.putExtra(Intent.EXTRA_STREAM, uri);

intent.setType("image/png");

startActivity(intent);

[3].[代碼] 通過瀏覽器開啟網頁

1

2

3

4

// 開啟Google首頁

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

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

startActivity(intent);

[4].[代碼] 寄送電子郵件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// 給[email protected]發郵件

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

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

startActivity(intent);

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

Intent intent = new Intent(Intent.ACTION_SEND);

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

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

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

intent.setType("text/plain");

startActivity(intent);

// 給多人發郵件

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_SUBJECT, "Subject");

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

intent.setType("message/rfc822");

startActivity(intent);

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

1

2

3

4

5

6

7

8

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

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

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

startActivity(intent);

// 路徑規劃:從北京某地(北緯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);

startActivity(intent);

[6].[代碼] 播放多媒體

1

2

3

4

5

6

7

8

Intent intent = new Intent(Intent.ACTION_VIEW);

Uri uri = Uri.parse("file:///sdcard/foo.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);

[7].[代碼] 拍照 

1

2

3

4

5

6

// 開啟拍照程式

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

startActivityForResult(intent, 0);

// 取出照片資料

Bundle extras = intent.getExtras();

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

[8].[代碼] 擷取並剪下圖片 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// 擷取並剪下圖片

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");// 返回格式

startActivityForResult(intent, 0);

// 剪下特定圖片

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"));

startActivityForResult(intent, 0);

[9].[代碼] 開啟Google Market

1

2

3

4

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

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

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

startActivity(intent);

[10].[代碼] 安裝和卸載程式 

1

2

3

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

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

startActivity(intent);

[11].[代碼] 進入設定介面 

1

2

3

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

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

startActivityForResult(intent, 0);

Android Intent 用法全面總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.