Android Intent的幾種用法全面總結

2021-08-25 10:02:31 字數 4276 閱讀 3754

intent 應該算是android中特有的東西。你可以在intent中指定程式 要執行的動作(比如:view,edit,dial),以及程式執行到該動作時所需要的資料 。都指定好後,只要呼叫startactivity(),android系統 會自動 尋找最符合你指定要求的應用 程式,並執行該程式。

下面列出幾種intent的用法

顯示網頁:

uri uri = uri.parse("");

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

intent it = new intent(intent.action_view,uri);

startactivity(it);

uri uri = uri.parse("tel:******");

intent it = new intent(intent.action_dial, uri);

startactivity(it);

uri uri = uri.parse("tel.******");

intent it =new intent(intent.action_call,uri);

要使用這個必須在配置檔案 中加入android .permission.call_phone" />

傳送sms/mms

呼叫傳送簡訊 的程式

intent it = new intent(intent.action_view);

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

傳送彩信

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

傳送email

uri uri = uri.parse("mailto:***@abc.com");

intent it = new intent(intent.action_sendto, uri);

startactivity(it);

intent it = new intent(intent.action_send);

it.putextra(intent.extra_email, "[email protected]");

it.putextra(intent.extra_text, "the email body text");

it.settype("text/plain");

startactivity(intent.createchooser(it, "choose email client"));

intent it=new intent(intent.action_send);

string tos=;

string ccs=;

it.putextra(intent.extra_email, tos);

it.putextra(intent.extra_cc, ccs);

it.putextra(intent.extra_text, "the email body text");

it.putextra(intent.extra_subject, "the email subject text");

it.settype("message/rfc822");

startactivity(intent.createchooser(it, "choose email client"));

新增附件

intent it = new intent(intent.action_send);

it.putextra(intent.extra_subject, "the email subject text");

it.putextra(intent.extra_stream, "file:///sdcard/mysong.***");

sendintent.settype("audio/***");

startactivity(intent.createchooser(it, "choose email client"));

intent it = new intent(intent.action_view);

uri uri = uri.parse("file:///sdcard/song.***");

it.setdataandtype(uri, "audio/***");

startactivity(it);

intent it = new intent(intent.action_view, uri);

startactivity(it);

uninstall 程式

uri uri = uri.fromparts("package", strpackagename, null);

intent it = new intent(intent.action_delete, uri);

startactivity(it);

不錯,我再補充幾個:

uninstall apk

uri uninstalluri = uri.fromparts("package", "***", null);

returnit = new intent(intent.action_delete, uninstalluri);

install apk

uri installuri = uri.fromparts("package", "***", null);

returnit = new intent(intent.action_package_added, installuri);

play audio

uri playuri = uri.parse("file:///sdcard/download/everything.***");

returnit = new intent(intent.action_view, playuri);

傳送附件

intent it = new intent(intent.action_send);

it.putextra(intent.extra_subject, "the email subject text");

it.putextra(intent.extra_stream, "file:///sdcard/eoe.***");

sendintent.settype("audio/***");

startactivity(intent.createchooser(it, "choose email client"));

uri uri = uri.parse("market://search?q=pname:pkg_name");

intent it = new intent(intent.action_view, uri);

startactivity(it);

intent it = new intent(intent.action_view, uri);

startactivity(it);

//page, and notice the id from the address bar

Android Intent的幾種用法

intent 應該算是android中特有的東西。你可以在intent中指定 程式要執行的動作 比如 view,edit,dial 以及程式執行到該動作時所需要的 資料。都指定好後,只要呼叫startactivity android系統會 自動尋找最符合你指定要求的 應用程式,並執行該程式。下面列出...

Android Intent 的幾種跳轉

一 不需要返回值的跳轉 intent intent new intent intent.setclass 目前的acitivy.this,目標activity.class startactivity intent 二 需要返回值的跳轉 intent intent new intent intent....

android intent 用法總結

給移動客服10086撥打 uri uri uri.parse tel 10086 intent intent new intent intent.action dial,uri startactivity intent 給10086傳送內容為 hello 的簡訊 uri uri uri.parse ...