Intent只能用來傳遞參數用那就大錯特錯了,下面介紹怎麼使用萬能的Intent幫我們
簡單的就能做很多的事情了。
Activity跳轉-無參數時
Intent it = new Intent(Activity1.this, Activity2.class); startActivity(it);
Activity跳轉-有參數時
Intent it = new Intent(Activity1.this, Activity2.class); Bundle bundle=new Bundle(); bundle.putString("ID", "This is Activity1 message!"); it.putExtras(bundle); startActivity(it);
參數接取方的資料處理程式碼
Bundle bundle=getIntent().getExtras(); String name=bundle.getString("ID");
向上一個Activity返回結果
Intent intent=getIntent(); Bundle bundle=new Bundle(); bundle.putString("ID", "This is from Activity2!"); intent.putExtras(bundle); setResult(RESULT_OK, intent);
返回結果的處理函數
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode==REQUEST_CODE){ if(resultCode==RESULT_CANCELED) setTitle("cancle"); else if (resultCode==RESULT_OK) { String temp=null; Bundle bundle=data.getExtras(); if(bundle!=null) temp=bundle.getString("ID"); setTitle(temp); } } }
瀏覽網頁
Uri uri = Uri.parse("http://jim690701.blogspot.tw/"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
顯示地圖
//輸入經緯度座標呼叫地圖 Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
路徑規劃
Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
撥打電話
//注意:使用撥號必須在AndroidManifest.xml 中,加上 //<uses-permission id="android.permission.CALL_PHONE" /> //呼叫撥號程序 Uri uri = Uri.parse("tel:0800000123"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); //直接撥號 Uri uri = Uri.parse("tel:0800000123"); Intent it = new Intent(Intent.ACTION_CALL, uri); startActivity(it);
寄送SMS/MMS簡訊
//呼叫簡訊程序 Intent it = new Intent(Intent.ACTION_VIEW, uri); it.putExtra("sms_body", "The SMS text"); it.setType("vnd.android-dir/mms-sms"); startActivity(it); //傳送簡訊 Uri uri = Uri.parse("smsto://0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "The SMS text"); startActivity(it); //傳送MMS彩訊 //取得手機內圖片連結 Uri uri = Uri.parse("content://media/external/images/media/23"); Intent it = new Intent(Intent.ACTION_SEND); //簡訊主題 it.putExtra("sms_body", "some text"); it.putExtra(Intent.EXTRA_STREAM, uri); it.setType("image/png"); startActivity(it);
傳送E-mail
//呼叫mail程序 Uri uri = Uri.parse("mailto:xxx@abc.com"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); startActivity(it); //簡易寄送E-mail Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com"); it.putExtra(Intent.EXTRA_TEXT, "The email body text"); it.setType("text/plain"); startActivity(Intent.createChooser(it, "Choose Email Client")); //一般寄送E-mail Intent it=new Intent(Intent.ACTION_SEND); String[] tos={"me@abc.com"}; String[] ccs={"you@abc.com"}; //主要寄送對象 it.putExtra(Intent.EXTRA_EMAIL, tos); //cc對象 it.putExtra(Intent.EXTRA_CC, ccs); //Email內容 it.putExtra(Intent.EXTRA_TEXT, "The email body text"); //Email主題 it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); it.setType("message/rfc822"); startActivity(Intent.createChooser(it, "選擇電子郵件客戶端")); //寄送附件 Intent it = new Intent(Intent.ACTION_SEND); //Email主題 it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); //附件 it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); //檔案格式 sendIntent.setType("audio/mp3"); startActivity(Intent.createChooser(it, "選擇電子郵件客戶端"));
播放多媒體
//播放手機內的MP3 Uri uri = Uri.parse("file:///sdcard/song.mp3"); Intent it = new Intent(Intent.ACTION_VIEW, uri); it.setType("audio/mp3"); startActivity(it); //播放內存的第一個鈴聲 Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);
google play應用
//尋找某個應用 Uri uri = Uri.parse("https://play.google.com/store/search?q=jim.memorandum"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //https://play.google.com/store/search?q=這段都一樣 //主要是q後面的搜尋條件,可以是package也可以是名稱 //顯示某個應用的相關信息 Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=jim.memorandum"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //https://play.google.com/store/apps/details?id=這段都一樣 //主要是id後面的package
Uninstall應用程式
//反安裝必須知道package name Uri uri = Uri.fromParts("package", strPackageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivity(it);
讀取QR Code
叫用條碼掃描器
Intent intent = new Intent("com.google.zxing.client.android.SCAN");//開啟條碼掃描器 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");//設定QR Code參數 startActivityForResult(intent, 1);//要求回傳1,回傳值可以自由設定
接收掃描後的回傳值
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1) { //startActivityForResult回傳值,回傳值是由叫用時帶入的設定 if (resultCode == RESULT_OK) { String contents = data.getStringExtra("SCAN_RESULT");//取得QR Code內容 txt_hello.setText(contents);//顯示儲存值 } } }
後記:
看完以上的Intent應用是不是覺得Intent可以用到的地方還真不少吧
我個人是覺得Intent超好用的需要什麼都可以用Intent去呼叫就好,
簡單又能節省程式碼,以上是對萬用Intent的介紹,謝謝。
請問大大,播放手機內的MP3是可以自選自己的音樂嗎?
回覆刪除可以!
刪除Uri uri = Uri.parse("file:///sdcard/song.mp3");
file:///sdcard/song.mp3指的是mp3檔案的路徑
只要改變這個路徑就能自選mp3
請問關於開啟網址的部分,
回覆刪除Uri uri = Uri.parse("http://jim690701.blogspot.tw/");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
網址的部分可以隨著我資料庫撈出來的資料而變動嗎??
如果可以我要怎麼弄呢??我新增了變數想套入變成這樣Uri.parse("http:"+urlweb);
但是卡在我不知道怎麼去把addesses內的資料不知道怎麼指派給urlweb...
我的程式碼局部長這樣:
ArrayList addesses = (ArrayList) this.getIntent()
.getSerializableExtra("hospital");
hospitalinfo.setText(addesses.get(0));
addressinfo.setText(addesses.get(1));
telinfo.setText(addesses.get(2));
web.setText(addesses.get(3));
}
private String urlweb;
private String phone;
SensorEventListener listener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
if (values[1] <= -9.5) {
// 若顯示網頁的內容是"(addesses.get(3));這個陣列內的資料(網址)"
Uri l_uri = Uri.parse("http:"+urlweb);
Intent l_intent = new Intent(Intent.ACTION_VIEW, l_uri);
startActivity(l_intent);