Android系統中標準Intent的使用

一 Android系統用於Activity的標準Intent


1 根據聯繫人ID顯示聯繫人信息
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);   //顯示聯繫人信息
		intent.setData(Uri.parse("content://contacts/people/492"));
		startActivity(intent);

2 根據聯繫人ID顯示撥號面板
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_DIAL);  //顯示撥號面板
		intent.setData(Uri.parse("content://contacts/people/492"));
		startActivity(intent);

3 顯示撥號面板, 並在撥號面板上將號碼顯示出來
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);   
		intent.setData(Uri.parse("tel://15216448315"));
		startActivity(intent);

4 顯示撥號面板, 並在撥號面板上將號碼顯示出來
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_DIAL);   //顯示撥號面板, 並在撥號面板上將號碼顯示出來
		intent.setData(Uri.parse("tel://15216448315"));
		startActivity(intent);

5 根據聯繫人的ID編輯聯繫人
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_EDIT);   //編輯聯繫人
		intent.setData(Uri.parse("content://contacts/people/492"));
		startActivity(intent);

6 顯示通信錄聯繫人和其餘帳號聯繫人的列表
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_VIEW);   
		intent.setData(Uri.parse("content://contacts/people/"));
		startActivity(intent);

7 啓動HomeScreen
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_MAIN);     //啓動HomeScreen
		intent.addCategory(Intent.CATEGORY_HOME);
		startActivity(intent);

8 選擇某個聯繫人的號碼,返回一個表明這個號碼的uri,如:content://contacts/phones/982
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);     
		intent.setType("vnd.android.cursor.item/phone");
		startActivityForResult(intent, 1);

9  打開多個應用選取各類類型的數據,以uri返回。返回的uri可以使用ContentResolver.openInputStream(Uri)打開
    該功能可用在郵件中附件的選取
    舉例以下:
    選取一張圖片, 返回的uri爲 content://media/external/images/media/47
    選取一首歌, 返回的uri爲 content://media/external/audio/media/51
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_GET_CONTENT);     
		intent.setType("*/*");
		intent.addCategory(Intent.CATEGORY_OPENABLE);
		startActivityForResult(intent, 2);

10 自定義一個chooser,不使用系統的chooser
     該chooser能夠有本身的標題(Title)
     而且沒必要讓用戶指定偏好
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_CHOOSER); 
		intent.putExtra(Intent.EXTRA_TITLE, "my chooser");
		intent.putExtra(Intent.EXTRA_INTENT, 
				new Intent(Intent.ACTION_GET_CONTENT)
				.setType("*/*")
				.addCategory(Intent.CATEGORY_OPENABLE)
				);
		
		startActivityForResult(intent, 2);

11 選取activity,返回的activity可在返回的intent.getComponent()中獲得
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_PICK_ACTIVITY); 
		intent.putExtra( Intent.EXTRA_INTENT, 
				new Intent(Intent.ACTION_GET_CONTENT)
				.setType("*/*")
				.addCategory(Intent.CATEGORY_OPENABLE)
				);
		startActivityForResult(intent, 3);

12 啓動搜索,在如下示例代碼中,"ANDROID"爲要搜索的字符串
     當執行這段代碼後, 會在系統的Chooser中顯示能夠用於搜索的程序列表
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_SEARCH);     //啓動搜索
		intent.putExtra(SearchManager.QUERY, "ANDROID");
		startActivity(intent);

13 啓動WEB搜索,在如下示例代碼中,"ANDROID"爲要搜索的字符串
     當執行這段代碼後, 會在系統的Chooser中顯示能夠用於搜索的程序列表,通常狀況下系統中安裝的瀏覽器都會顯示出來
Intent intent = new Intent();
		intent.setAction(Intent.ACTION_WEB_SEARCH);     //啓動搜索
		intent.putExtra(SearchManager.QUERY, "ANDROID");
		startActivity(intent);


二  Android系統用於BroadcastReceiver的標準Intent


1 ACTION_TIME_TICK,系統時鐘廣播,系統每分鐘都會發送一個這樣的廣播,
   若是在應用開發中,有些邏輯依賴於系統時鐘,能夠註冊一個廣播接收者
   這是一個受保護的action,只有系統才能發送這個廣播
   而且,在manifest文件中註冊的廣播接收者不能接收到該廣播,若要接收該廣播,必須在代碼中註冊廣播接收者
registerReceiver(new BroadcastReceiver(){

			@Override
			public void onReceive(Context context, Intent intent) {
				Log.i("xxxx", "TIME_TICK");
			}
			
		}, 
		new IntentFilter(Intent.ACTION_TIME_TICK));

2 在官方文檔中,列出瞭如下標準的廣播action


三  Android中的標準類別(category)


類別(category)通常配合action使用,如下爲系統中的標準類別,因爲數量過多,只能在使用到時再詳細研究

這些常量用於在調用Intent.putExtra(String, Bundle)時做爲鍵值傳遞數據,一樣因爲數量較多,在此只列出索引

Intent類中定義了一些以FLAG_開頭的標誌位,這些標誌位中有的很是重要,會影響app中Activity和BroadcastReceiver等的行爲。
如下爲這些標誌位的索引,是從官方文檔上的截圖。以後會對重要的標誌加以詳細分析

相關文章
相關標籤/搜索