2008年12月29日 星期一

Intent 用法大公開

How to use Intent to send an email, SMS, open a web browser, show map, etc.?

Intent 應該算是 Andorid 中特有的東西。你可以在 Intent 中,指定要應用程式執行的動作 (view, edit, dial),以及應用程式執行該動作時,所需要的資料。都指定好後,只要透過 startActivity(),Android 系統會自動尋找,最符合你指定要求的應用程式,並喚起執行該應用程式。

不過,這部份的文件還不是很完整。Reference of Available Intents 有列一些。底下是我收集的一些用法,分享出來給有需要的你。有些還沒有實際驗證過,如果發現有錯誤,或有新的用法,也請告訴我。

顯示網頁

顯示地圖

路徑規劃

撥打電話

傳送 SMS/MMS

傳送 Email

顯示聯絡人清單

顯示某個朋友的詳細資料

播放多媒體

從圖庫中回傳選到的圖片

啟動照相機,並將相片存在指定的檔案中

Market 相關

Uninstall 應用程式

安裝 APK 檔

29 則留言:

Unknown 提到...

very useful
thanks

Jason 提到...

太棒了, 從這裡學蠻多的; 感謝大大

Jason 提到...

報告:

傳送簡訊 ==> 我試不出來, 都會直接開啟空白的簡訊傳送介面...

samlu 提到...

注意是 sms_body
There is a underline character between "sms" and "body".

Unknown 提到...

這一篇文章很有用哦... ^__^

不過想問一個問題啊, 我有看到在傳送MMS的部份, 是傳一個image, 現在android可以傳聲音檔嗎?? 我用image的那個方法改成傳聲音檔, 試了好一陣子, 好像都會error耶... @@ 不知道是那裡寫錯了... 還請大大指點一番囉... ^^

Jason 提到...

samlu, 謝謝提醒....我後來就改用SmsManager來發送...比較習慣; 謝謝您

samlu 提到...

To Andrea,
你這行 it.setType("image/png"); 有改嗎?
Don't forget to set a proper MIME type for the attached file.

Unknown 提到...
作者已經移除這則留言。
Unknown 提到...

我有改耶...
我的寫法如下
Uri uri = Uri.parse("content://media/external/audio/media/12");
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra("sms_body", "some text");
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("audio/amr");
startActivity(sendIntent);

url是我在儲存錄音檔的時候存入的url content. 不知道是不是path出了錯... 執行時會說我這個AP has stopped unexpectedly. 我再check一下囉... thanks! ^__^

匿名 提到...

你好
Uri uri = Uri.parse("file:///sdcard/song.mp3");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.setType("audio/mp3");
startActivity(it);
这样放audio好像调不起来music播放器,是不是新版本有些什么改动?
请指教,谢谢

samlu 提到...

原先的寫法的確在新版中有問題。我已經更正這個問題,並驗證正確。請參照現在的寫法。

samlu 提到...

另外,查了 mms 的原始碼,目前用 SEND 的方式只支援 image/*, video/* and text/plain

匿名 提到...

I think www.cyrket.com use
//尋找某個應用程式
Uri uri = Uri.parse("market://search?q=pname:pkg_name");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where pkg_name is the full package path for an application
to list all Android Market Application. Can you do that?

匿名 提到...

//顯示某應用程式詳細畫面
Uri uri = Uri.parse("market://details?id=app_id");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
//where app_id is the application ID, find the ID
//by clicking on your application on Market home
//page, and notice the ID from the address bar
--这个app_id好像看不到了?

匿名 提到...

請問一下在1.6版的SDK配上JDK6build16
"geo:25.047192,121.516981?z=zoom"
會有 "Unable to load the URL" 的訊息出現

匿名 提到...

自己找到了
zoom 是一個數值, 不是 keyword, 希望有幫助

匿名 提到...

請問一下 我在程式中寫入
Uri uri = Uri.parse("geo:38.899533,-77.036476");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);

程式在 Emulator上執行會crash.可否請問為何呢?

samlu 提到...

我猜你所啟動的 emulator 是不含地圖程式的。
不要假設所有的手機都有地圖程式,因此要加個 try/catch 做個錯誤處理。

John 提到...

你好我用下面的方式想要用MMS傳送一個vcard可是為什麼他還是叫出一般的email client?啟動MMS跟Email intent的差別在哪?
是差在"it.putExtra("sms_body", "hello world");"嗎??謝謝回答
Intent it = new Intent(Intent.ACTION_SEND);
File file=new File("/sdcard/contacts.vcf");
it.putExtra("sms_body", "hello world");
it.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
it.setType("text/x-vcard");
startActivity(it);

匿名 提到...

請問一下!
如果播放多媒體是一次要傳多首歌進去的話!
那該怎嚜寫呢?
我用陣列的方式行不通 ><"

samlu 提到...

應該是不支援一次傳多首歌進去的。

我會建議你在自己的介面上,有多首歌的列表,當使用者選某首時。再叫音樂播放器播放。或是參照音樂播放器的原始碼,自己寫,也是個方法。

匿名 提到...

您好,想請教一下
我的sdk安裝正常可以啟動
但是當我開啟書裡面的光碟範本,為何會出現一堆xx啊?
連進行基本體驗都沒辦法QQ

samlu 提到...

我沒有寫書,請你直接找作者。

匿名 提到...

作者沒留網站,也找不到啊QQ~

samlu 提到...

書中仔細看一下。應該有作者的網站。沒有的,就不要買了。
如果你買的是 蓋索林 的書,那他的網站是 http://sites.google.com/site/gasodroid/

匿名 提到...

我買的是旗標的書....

順便想請教一下,ANDROID的圖片所支援解析度大約是多少乘多少?

黃偉賢 提到...

你好,請問路徑規劃,要怎樣才能規劃出走路的而不是開車的呢?

Amanda 提到...

想請問一下,我使用
Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//假設你要將相片存在 /sdcard/xxx.jpg 中
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xxx.jpg");
it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivity(it, 0);


使用後會自動在DCIM資料夾中多存一份同樣的照片,請問這可以在那設定成不要存嗎?

david小飛俠 提到...

File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xxx.jpg");
startActivity(it, 0);
用上述的方法,開啟拍照功能. 拍照的照片檔,用手機去檢查,有存在指定的目錄下面, 但用PC連上後檔案總管內,卻看不看拍照的檔案, 其他的檔案卻可以看到, 不知問題出在那,不知大大有沒有遇過. 謝謝

張貼留言