Android_Intent意圖詳解

來源:互聯網
上載者:User

標籤:

        Intent是一個將要執行的動作的抽象的描述,由Intent來協助完成android各個組件之間的通訊。比如調用Activity執行個體化對象的startActivity()來啟動一個activity,或者由broadcaseIntent()來傳遞給所有感興趣的BroadcaseReceiver, 或者由startService()/bindservice()來啟動一個背景service。可見,intent主要用來啟動activity或者service(並攜帶需要傳遞的參數資訊),intent理解成activity之間的粘合劑。
總之,Intent具有啟用組件和攜帶資料的功能!2.Intent形式(1).顯(Explicit Intents)       明確指定組件名的Intent為顯式意圖,指定了Intent應該傳遞給那個組件。通過下面代碼方式,可以建立顯執行個體化對象,並設定需要傳遞的參數資訊。由於顯指定了具體的組件對象,不需要設定intent的其它意圖過濾對象。[java]  //  1.建立Intent執行個體化對象幾種方式    Intent intent = new Intent();  intent.setClass(Context packageContext, Class<?> cls) ;           //內部調用setComponent(ComponentName)  intent.setClassName(Context packageContext, String className) ; //內部調用setComponent(ComponentName)  intent.setClassName(String packageName, String className) ;     //內部調用setComponent(ComponentName),可以啟用外部應用    intent.setComponent(new ComponentName(this, Class<?> cls));  intent.setComponent(new ComponentName(this, "package Name"));  (2).隱式意圖(Implicit Intents)沒有明確指定組件名的Intent為隱式意圖,系統會根據隱式意圖中設定的 動作(action)、類別(category)、資料URI等來匹配最合適的組件。1).actionThe general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 包括Android 系統指定的 和 自訂[java]  intent.setAction("com.baidu.action.TEST");  [html] view plaincopy<action android:name="com.baidu.action.TEST"/>  2).dataexpressed as a Uri, The data to operate on, such as a person record in the contacts database.系統內建的Action簡單舉例Action Data(Uri) ContentACTION_VIEWcontent://contacts/people/1Display information about the person whose identifier is "1".ACTION_VIEWtel:123Display the phone dialer with the given number filled in.ACTION_DIALtel:123Display the phone dialer with the given number filled in. 自訂data匹配[java]  intent.setData(Uri.parse("baidu://www.baidu.com/news"));  [html]  <!-- android:path 內容字串需要以 / 開頭 -->  <data android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>  3).categoryGives additional information about the action to execute.注意:項目清單的xml檔案意圖過濾器中必須指定 android.intent.category.DEFAULT類別,Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity,or Context can‘t the acitivity component[java]  intent.addCategory("com.baidu.category.TEST");  [html] <!-- 必須指定CATEGORY_DEFAULT,只有這樣startActivity(intent)才能找到 -->  <category android:name="com.baidu.category.TEST" />  <category android:name="android.intent.category.DEFAULT" />  除了以上主要屬性外,下面還有其它屬性可以額外增強。4).typeSpecifies an explicit type (a MIME type) of the intent data.[java]  intent.setType("image/jpeg");  [html] view plaincopy<data android:mimeType="image/*" />  注意:java檔案中data Uri 和 type不能同時使用各自的函數進行設定,因為使用type時會把Uri清除掉,可以使用setDataAndType方法設定[java] intent.setDataAndType(Uri.parse("baidu://www.baidu.com/news"), "image/jpeg");  [html]  <data android:mimeType="image/*" android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>  (3).兩者的使用區別 顯式意圖一般在應用的內部使用,因為在應用內部已經知道了組件的名稱,直接調用就可以了。當一個應用要啟用另一個應用中的Activity時,只能使用隱式意圖,根據Activity配置的意圖過濾器建一個意圖,讓意圖中的各項參數的值都跟過濾器匹配,這樣就可以啟用其他應用中的Activity。所以,隱式意圖是在應用與應用之間使用的。3.Activity的Intent資料傳遞[java]  //Activity間的資料傳遞  //  1.直接向intent對象中傳入索引值對(相當於Intent對象具有Map索引值對功能)  intent.putExtra("first", text1.getText().toString());  intent.putExtra("second", text2.getText().toString());    //  2.建立一個Bundle對象 ,想該對象中加入索引值對,然後將該對象加入intent中  Bundle bundle = new Bundle();  bundle.putString("first", "zhang");  bundle.putInt("age", 20);  intent.putExtras(bundle);    //  3.向intent中添加ArrayList集合對象  intent.putIntegerArrayListExtra(name, value);  intent.putIntegerArrayListExtra(name, value);       //  4.intent傳遞Object對象(被傳遞的對象的類實現Parcelable介面,或者實現Serialiable介面)  public Intent putExtra(String name, Serializable value)  public Intent putExtra(String name, Parcelable value)   4.Activity退出的返回結果[java]  //  1.通過startActivityForResult方式啟動一個Activity  MainActivity.this.startActivityForResult(intent, 200);  //intent對象,和  requestCode請求碼    //  2.新activity設定setResult方法,通過該方法可以傳遞responseCode 和 Intent對象  setResult(101, intent2);                                //responseCode響應碼 和 intent對象    //  3.在MainActivity中覆寫onActivityResult方法,新activity一旦退出,就會執行該方法  protected void onActivityResult(int requestCode, int resultCode, Intent data) {      Toast.makeText(this, data.getStringExtra("info")+"requestCode:"+requestCode+"resultCode:"+resultCode, Toast.LENGTH_LONG).show();  }  5.Intent常見應用(轉)(1).調用撥號程式[java] Uri uri = Uri.parse("tel:10086");   Intent intent = new Intent(Intent.ACTION_DIAL, uri);   startActivity(intent);   (2).傳送簡訊或者多媒體訊息[java]  //發生簡訊  Uri uri = Uri.parse("smsto:10086");   Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   intent.putExtra("sms_body", "Hello");   startActivity(intent);     //發送多媒體訊息,相當於發送帶附件的簡訊  Intent intent = new Intent(Intent.ACTION_SEND);   intent.putExtra("sms_body", "Hello");   Uri uri = Uri.parse("content://media/external/images/media/23");   intent.putExtra(Intent.EXTRA_STREAM, uri);   intent.setType("image/png");   startActivity(intent);   (3).通過瀏覽器開啟網頁[java]  Uri uri = Uri.parse("http://www.google.com");   Intent intent  = new Intent(Intent.ACTION_VIEW, uri);   startActivity(intent);  (4).寄送電子郵件[java]  Uri uri = Uri.parse("mailto:[email protected]");   Intent intent = new Intent(Intent.ACTION_SENDTO, uri);   startActivity(intent);     //給[email protected]發郵件發送內容為“Hello”的郵件   Intent intent = new Intent(Intent.ACTION_SEND);   intent.putExtra(Intent.EXTRA_EMAIL, "[email protected]");   intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");   intent.putExtra(Intent.EXTRA_TEXT, "Hello");   intent.setType("text/plain");   startActivity(intent);     // 給多人發郵件   Intent intent=new Intent(Intent.ACTION_SEND);   String[] tos = {"[email protected]", "[email protected]"}; // 收件者   String[] ccs = {"[email protected]", "[email protected]"}; // 抄送   String[] bccs = {"[email protected]", "[email protected]"}; // 密送   intent.putExtra(Intent.EXTRA_EMAIL, tos);   intent.putExtra(Intent.EXTRA_CC, ccs);   intent.putExtra(Intent.EXTRA_BCC, bccs);   intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");   intent.putExtra(Intent.EXTRA_TEXT, "Hello");   intent.setType("message/rfc822");   startActivity(intent);   (5).顯示地圖與路徑規劃[java]  // 開啟Google地圖中國北京位置(北緯39.9,東經116.3)   Uri uri = Uri.parse("geo:39.9,116.3");   Intent intent = new Intent(Intent.ACTION_VIEW, uri);   startActivity(intent);     // 路徑規劃:從北京某地(北緯39.9,東經116.3)到上海某地(北緯31.2,東經121.4)   Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");   Intent intent = new Intent(Intent.ACTION_VIEW, uri);   startActivity(intent);   (6).播放多媒體[java Intent intent = new Intent(Intent.ACTION_VIEW);   Uri uri = Uri.parse("file:///sdcard/foo.mp3");   intent.setDataAndType(uri, "audio/mp3");   startActivity(intent);     Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");   Intent intent = new Intent(Intent.ACTION_VIEW, uri);   startActivity(intent);   (7).拍照[java]  // 開啟拍照程式   Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    startActivityForResult(intent, 0);     // 取出照片資料   Bundle extras = intent.getExtras();    Bitmap bitmap = (Bitmap) extras.get("data");   (8).擷取並剪下圖片[java]  // 擷取並剪下圖片   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);   intent.setType("image/*");   intent.putExtra("crop", "true"); // 開啟剪下   intent.putExtra("aspectX", 1); // 剪下的寬高比為1:2   intent.putExtra("aspectY", 2);   intent.putExtra("outputX", 20); // 儲存圖片的寬和高   intent.putExtra("outputY", 40);    intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 儲存路徑   intent.putExtra("outputFormat", "JPEG");// 返回格式   startActivityForResult(intent, 0);     // 剪下特定圖片   Intent intent = new Intent("com.android.camera.action.CROP");    intent.setClassName("com.android.camera", "com.android.camera.CropImage");    intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));    intent.putExtra("outputX", 1); // 剪下的寬高比為1:2   intent.putExtra("outputY", 2);   intent.putExtra("aspectX", 20); // 儲存圖片的寬和高   intent.putExtra("aspectY", 40);   intent.putExtra("scale", true);   intent.putExtra("noFaceDetection", true);    intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));    startActivityForResult(intent, 0);   (9).開啟Google Market[java]  // 開啟Google Market直接進入該程式的詳細頁面   Uri uri = Uri.parse("market://details?id=" + "com.demo.app");   Intent intent = new Intent(Intent.ACTION_VIEW, uri);   startActivity(intent);   (10).安裝和卸載程式[java]  Uri uri = Uri.fromParts("package", "com.demo.app", null);     Intent intent = new Intent(Intent.ACTION_DELETE, uri);     startActivity(intent);   (11).進入設定介面[java] // 進入無線網路設定介面(其它可以舉一反三)     Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);     startActivityForResult(intent, 0)

Android_Intent意圖詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.