Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

Intent 常用調用代碼大全(持續完善中)

編輯:初階開發

以下整理了網友歸納的 Intent 常見用法,涵蓋網頁、地圖、通話、簡訊、郵件、多媒體與應用安裝卸載等場景,方便大家隨時查閱。後續還將不斷補充完善。

顯示 Web 網頁:

  1. Uri uri = Uri.parse("https://www.android123.com.cn");
  2. Intent it = new Intent(Intent.ACTION_VIEW,uri);
  3. startActivity(it);

顯示 Google 地圖:

  1. Uri uri = Uri.parse("geo:38.899533,-77.036476");
  2. Intent it = new Intent(Intent.Action_VIEW,uri);
  3. startActivity(it);

Maps 路徑規劃:

  1. Uri uri = Uri.parse("https://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en");
  2. Intent it = new Intent(Intent.ACTION_VIEW,uri);
  3. startActivity(it);

撥打電話:

  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_DIAL, uri);
  3. startActivity(it);

直接撥打(需權限 <uses-permission id="android.permission.CALL_PHONE" />):

  1. Uri uri = Uri.parse("tel:xxxxxx");
  2. Intent it = new Intent(Intent.ACTION_CALL,uri);

發送 SMS/MMS:

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. it.putExtra("sms_body", "android開發網歡迎您");
  3. it.setType("vnd.android-dir/mms-sms");
  4. startActivity(it);

發送簡訊:

  1. Uri uri = Uri.parse("smsto:10086");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. it.putExtra("sms_body", "10086"); //正文為10086
  4. startActivity(it);

發送彩信:

  1. Uri uri = Uri.parse("content://media/external/images/media/10"); //該Uri根據實際情況修改,external代表外部存儲即sdcard
  2. Intent it = new Intent(Intent.ACTION_SEND);
  3. it.putExtra("sms_body", "android123.com.cn");
  4. it.putExtra(Intent.EXTRA_STREAM, uri);
  5. it.setType("image/png");
  6. startActivity(it);

發送 Email:

  1. Uri uri = Uri.parse("mailto:android123@163.com");
  2. Intent it = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(it);

  1. Intent it = new Intent(Intent.ACTION_SEND);
  2. it.putExtra(Intent.EXTRA_EMAIL, "android123@163.com");
  3. it.putExtra(Intent.EXTRA_TEXT, "android開發網測試");
  4. it.setType("text/plain");
  5. startActivity(Intent.createChooser(it, "選擇一個Email客戶端"));

帶抄送的郵件:

  1. Intent it=new Intent(Intent.ACTION_SEND);
  2. String[] tos={"android123@163.com"}; //發送到
  3. String[] ccs={"ophone123@163.com"}; //抄送
  4. it.putExtra(Intent.EXTRA_EMAIL, tos);
  5. it.putExtra(Intent.EXTRA_CC, ccs);
  6. it.putExtra(Intent.EXTRA_TEXT, "正文");
  7. it.putExtra(Intent.EXTRA_SUBJECT, "標題");
  8. it.setType("message/rfc822"); //編碼類型
  9. startActivity(Intent.createChooser(it, "選擇一個Email客戶端"));

Email 添加附件:

  1. Intent it = new Intent(Intent.ACTION_SEND);
  2. it.putExtra(Intent.EXTRA_SUBJECT, "正文");
  3. it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/nobody.mp3"); //附件為sd卡上的nobody MP3文件
  4. it.setType("audio/mp3");
  5. startActivity(Intent.createChooser(it, "選擇一個Email客戶端"));

播放多媒體:

  1. Intent it = new Intent(Intent.ACTION_VIEW);
  2. Uri uri = Uri.parse("file:///sdcard/nobody.mp3");
  3. it.setDataAndType(uri, "audio/mp3");
  4. startActivity(it);

從系統內部的 MediaProvider 索引調用播放:

  1. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);

卸載程式:

  1. Uri uri = Uri.fromParts("package", packageName, null); //packageName為包名,比如com.android123.apkInstaller
  2. Intent it = new Intent(Intent.ACTION_DELETE, uri);
  3. startActivity(it);

安裝 APK:

安裝 APK 檔案的方法有多種,可參考官方相關文檔中介紹的兩種實現方式,根據實際需求靈活選用即可。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved