這裡要注意的是,"tel:"是必要的,不然會有錯誤
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("tel:1234567890")); startActivity(intent);
● 直接撥打電話(make a phone call)
如果不想要顯示撥號畫面,想直接撥打電話,則需要使用ACTION_CALL
Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:0123456789")); startActivity(intent);因為有使用到ACTION_CALL,所以要在manifest加入權限
<uses-permission android:name="android.permission.CALL_PHONE" />
● 呼叫瀏覽器(Browser)
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.acer.com")); startActivity(intent);
加入搜尋字串
Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH); // 設定搜尋的字串 intent.putExtra(SearchManager.QUERY, "acer"); startActivity(intent);
● 呼叫相機(Camera)
Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1);取得拍攝後的照片
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != RESULT_OK) { // 無法得到正確結果時會跑到這裡 return; } if (requestCode == 1) { // 這個bitmap是拍攝之照片的資料 Bitmap bitmap = (Bitmap) data.getExtras().get("data"); // 在LAYOUT拉一個ImageView,來顯示照片 ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageBitmap(bitmap); } }
● 呼叫錄音機(Recorder)
Intent intent = new Intent(); intent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION); startActivity(intent);
● 呼叫Market應用程式(call market application)
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=comq.android.autoRedial")); startActivity(intent);
● 顯示聯絡人清單
Intent intent = new Intent(Intent.ACTION_VIEW, People.CONTENT_URI); startActivity(intent );顯示第3筆聯絡人的詳細資料
Uri uriContact = ContentUris.withAppendedId(People.CONTENT_URI, 3); //3 是聯絡人ID Intent intent = new Intent(Intent.ACTION_VIEW, uriContact ); startActivity(intent );
● 顯示地圖
Uri uri = Uri.parse("geo:25.718058,-100.279487"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); //其他geo的用法 //geo:latitude,longitude <--上面例子是這個 //geo:latitude,longitude?z=zoom //geo:0,0?q=business+near+city //google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom //geo:0,0?q=my+street+address路徑規劃
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 );
延伸閱讀: 傳送郵件(Send mail)