一、Intent作用
android 中intent是經常要用到的。不管是頁面調轉,還是傳遞資料,或是調用外部程式,系統功能都要用到intent。
Android的有三個基本組件——Activity,Service和BroadcastReceiver,它們都是通過Intent機制啟用的,而不同類型的組件有傳遞Intent的不同方式。
(1) 要啟用一個新的Activity,或者讓一個現有的Activity執行新的操作,可以通過調用Context.startActivity()或者Activity.startActivityForResult()方法。這兩個方法需要傳入的Intent參數也稱為Activity Action Intent(活動行為意圖),根據Intent對象對目標Activity描述的不同,來啟動與之相匹配的Activity或傳遞資訊。
(2) 要啟動一個新的服務,或者向一個已有的服務傳遞新的指令,調用Context.startService()方法或調用Context.bindService()方法將調用此方法的內容物件與Service綁定。
(3) 通過Context.sendBroadcast()、Context.sendOrderBroadcast()和Context.send-StickBroadcast()這三個方法可以發送BroadcastIntent。BroadcastIntent發送後,所有登入的擁有與之相匹配IntentFilter的BroadcastReceiver就會被啟用。這種機制被廣泛運用於裝置或系統狀態變化的通知,一個常見的例子是,當Android的電池電量過低時,系統會發送Action為BATTERY_LOW的廣播,接著任何可匹配該Action的IntentFilter註冊的BroadcastReceiver都會各自運行自訂的處理代碼,比如關閉裝置的WIFI和GPS以節省電池消耗。
Intent一旦發出,Android都會準確找到相匹配的一個或多個Activity、Service或Broadcast-Receiver作為響應。所以,不同類型的Intent訊息不會出現重疊:BroadcastIntent訊息只會發送給BroadcastReceiver,而絕不可能發送給Activity或Service。由startActivity()傳遞的訊息也只可能發送給Activity,由startService()傳遞的Intent只可能發送給Service。
二、使用Intent在多個Activity之間傳遞資料
我們先重點講解如何使用Intent在多個Activity之間傳遞資料,根據前面的介紹我們應該已經清楚,要從Activity1傳遞資料到Activity2重點是startActivity()和startActivityForResult()兩個方法。
1.無參數Activity跳轉
Intent it = new Intent(Activity1.this, Activity2.class);
startActivity(it);
方法1 實現從Activity1直接切換到Activity2.其中 Activity1和Activity2為視窗1和視窗2的類名,注意:這兩個Activity一定要在AndroidManifest.xml中註冊了才能開啟。
2.向下一個Activity傳遞資料(使用Bundle和Intent.putExtras)
Intent it = new Intent(Activity1.this, Activity2.class);
Bundle bundle=new Bundle();
bundle.putString("name", "This is from MainActivity!");
it.putExtras("bd",bundle); // it.putExtra(“test”, "shuju”);
startActivity(it); // startActivityForResult(it,REQUEST_CODE);
方法2和方法1類似,但是實現了資料傳遞,注意Bundle對象類似於HashTable,可以存放一個鍵和對應的對象。而Intent對象也可以以索引值對的方式存放bundle對象,從而實現在Activity1和
Acitivty2之間傳遞資料。在Activity2中可以通過以下方法獲得Activity1中傳遞過來的資料
Intent intent = getIntent();
Bundle bd = intent.getBundleExtra("bd");// 根據bundle的key得到對應的對象
String name=bd.getString("name");
3.在Activity2中向上一個Activity返回結果(使用setResult,針對startActivityForResult(it,REQUEST_CODE)啟動的Activity)
Intent intent=getIntent();
Bundle bundle2=new Bundle();
bundle2.putString("name", "This is from ShowMsg!");
intent.putExtras(bundle2);
setResult(RESULT_OK, intent);
4.Activity1中如果要根據Activity2中傳入的參數進行處理,必須在Activity1中的onActivityResult進行處理
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
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("name");
setTitle(temp);
}
}
}
三、隱式Intent和運行時綁定
隱式Intent是一種讓匿名應用程式元件服務動作請求的機制。當建立一個新的隱式Intent時,你指定要執行的動作,作為可選項,你可以提供這個動作所需的資料。
當你使用這個新的隱式Intent來啟動Activity時,Android會在運行時解析它,找到最適合在指定的資料類型上執行動作的類。這意味著,你可以建立使用其它應用程式的工程,而不需要提前精確地知道你會借用哪個應用程式的功能。
例如,如果你想讓使用者在應用程式裡打電話,與其實現一個新的撥號,不如使用一個隱式的Intent來請求一個在一個電話號碼(URI表示)上的動作(撥一個號碼),如下程式碼片段所示:
if (somethingWeird && itDontLookGood)
{
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(“tel:555-2368”));
startActivity(intent);
}
Android解析這個Intent並啟動一個提供了能在一個號碼上執行撥號動作的Activity,在這裡,是撥號Activity。
下面是轉載來的其他的一些Intent用法執行個體(轉自javaeye)
顯示網頁
Uri uri = Uri.parse("http://google.com/");
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);
//其他 geo URI 範例
//geo:latitude,longitude
//geo:latitude,longitude?z=zoom
//geo:0,0?q=my+street+address
//geo:0,0?q=business+near+city
//google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom
路徑規劃
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);
//where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
打電話
//叫出撥號程式
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);
//用這個,要在 AndroidManifest.xml 中,加上
//<uses-permission id="android.permission.CALL_PHONE" />
傳送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);
傳送 Email
Uri uri = Uri.parse("mailto:xxx@abc.com");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(it);
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"));
Intent it=new Intent(Intent.ACTION_SEND);
String[] tos={"me@abc.com"};
String[] ccs={"you@abc.com"};
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, "http://www.cnblogs.com/l_dragon/admin/file:///sdcard/mysong.mp3");
sendIntent.setType("audio/mp3");
startActivity(Intent.createChooser(it, "Choose Email Client"));
播放多媒體
Uri uri = Uri.parse("http://www.cnblogs.com/l_dragon/admin/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);
Market 相關
//尋找某個應用
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
//顯示某個應用的相關資訊
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
Uninstall 應用程式
Uri uri = Uri.fromParts("package", strPackageName, null);
Intent it = new Intent(Intent.ACTION_DELETE, uri);
startActivity(it);