欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中的Intent對(duì)象完全解析

 更新時(shí)間:2016年04月24日 11:01:08   作者:劍蕭舞蝶  
這篇文章主要介紹了Android中的Intent對(duì)象,深入講解了intent對(duì)象傳遞消息的各種用法,需要的朋友可以參考下

一、 Intent 作用
Intent 是一個(gè)將要執(zhí)行的動(dòng)作的抽象的描述,一般來(lái)說(shuō)是作為參數(shù)來(lái)使用,由Intent來(lái)協(xié)助完成Android各個(gè)組件之間的通訊。比如說(shuō)調(diào)用startActivity()來(lái)啟動(dòng)一個(gè)activity,或者由broadcaseIntent()來(lái)傳遞給所有感興趣的BroadcaseReceiver, 再或者由startService()/bindservice()來(lái)啟動(dòng)一個(gè)后臺(tái)的service.所以可以看出來(lái),intent主要是用來(lái)啟動(dòng)其他的activity 或者service,所以可以將intent理解成activity之間的粘合劑。

二、 Intent的構(gòu)成
要在不同的activity之間傳遞數(shù)據(jù),就要在intent中包含相應(yīng)的東西,一般來(lái)說(shuō)數(shù)據(jù)中最基本的應(yīng)該包括:
Action 用來(lái)指明要實(shí)施的動(dòng)作是什么,比如說(shuō)ACTION_VIEW, ACTION_EDIT等。具體的可以查閱android SDK-> reference中的Android.content.intent類,里面的constants中定義了所有的action。
Data 要事實(shí)的具體的數(shù)據(jù),一般由一個(gè)Uri變量來(lái)表示

下面是一些簡(jiǎn)單的例子:

ACTION_VIEW content://contacts/1 //顯示identifier為1的聯(lián)系人的信息。
ACTION_DIAL content://contacts/1 //給這個(gè)聯(lián)系人打電話

除了Action和data這兩個(gè)最基本的元素外,intent還包括一些其他的元素,
Category(類別): 這個(gè)選項(xiàng)指定了將要執(zhí)行的這個(gè)action的其他一些額外的信息,例如 LAUNCHER_CATEGORY 表示Intent 的接受者應(yīng)該在Launcher中作為頂級(jí)應(yīng)用出現(xiàn);而ALTERNATIVE_CATEGORY表示當(dāng)前的Intent是一系列的可選動(dòng)作中的一個(gè),這些動(dòng)作可以在同一塊數(shù)據(jù)上執(zhí)行。具體同樣可以參考android SDK-> reference中的Android.content.intent類。以前我也寫過(guò)一篇于category有關(guān)的文章,點(diǎn)擊這里可以查看。
Type(數(shù)據(jù)類型): 顯式指定Intent的數(shù)據(jù)類型(MIME)。一般Intent的數(shù)據(jù)類型能夠根據(jù)數(shù)據(jù)本身進(jìn)行判定,但是通過(guò)設(shè)置這個(gè)屬性,可以強(qiáng)制采用顯式指定的類型而不再進(jìn)行推導(dǎo)。
component(組件): 指定Intent的的目標(biāo)組件的 類名稱。通常 Android會(huì)根據(jù)Intent 中包含的其它屬性的信息,比如action、data/type、category進(jìn)行查找,最終找到一個(gè)與之匹配的目標(biāo)組件。但是,如果 component這個(gè)屬性有指定的話,將直接使用它指定的組件,而不再執(zhí)行上述查找過(guò)程。指定了這個(gè)屬性以后,Intent的其它所有屬性都是可選的。
extras(附加信息),是其它所有附加信息的集合。使用extras可以為組件提供擴(kuò)展信息,比如,如果要執(zhí)行“發(fā)送電子郵件”這個(gè)動(dòng)作,可以將電子郵件的標(biāo)題、正文等保存在extras里,傳給電子郵件發(fā)送組件。
下面是這些額外屬性的幾個(gè)例子:
ACTION_MAIN with category CATEGORY_HOME //用來(lái) Launch home screen. 以前我也寫過(guò)一篇于與之有關(guān)的文章, 點(diǎn)擊這里可以看到。
ACTION_GET_CONTENT with MIME type vnd.android.cursor.item/phone //用來(lái)列出列表中的所有人的電話號(hào)碼
綜上可以看出,action、 data/type、category和extras 一起形成了一種語(yǔ)言,這種語(yǔ)言可以是android可以表達(dá)出諸如“給張三打電話”之類的短語(yǔ)組合。

三、 intent的解析
應(yīng)用程序的組件為了告訴Android自己能響應(yīng)、處理哪些隱式Intent請(qǐng)求,可以聲明一個(gè)甚至多個(gè)Intent Filter。每個(gè)Intent Filter描述該組件所能響應(yīng)Intent請(qǐng)求的能力——組件希望接收什么類型的請(qǐng)求行為,什么類型的請(qǐng)求數(shù)據(jù)。比如之前請(qǐng)求網(wǎng)頁(yè)瀏覽器這個(gè)例子中,網(wǎng)頁(yè)瀏覽器程序的Intent Filter就應(yīng)該聲明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及與之相關(guān)的請(qǐng)求數(shù)據(jù)是網(wǎng)頁(yè)地址URI格式。如何為組件聲明自己的Intent Filter? 常見的方法是在AndroidManifest.xml文件中用屬性< Intent-Filter>描述組件的Intent Filter。
前面我們提到,隱式Intent(Explicit Intents)和Intent Filter(Implicit Intents)進(jìn)行比較時(shí)的三要素是Intent的動(dòng)作、數(shù)據(jù)以及類別。實(shí)際上,一個(gè)隱式Intent請(qǐng)求要能夠傳遞給目標(biāo)組件,必要通過(guò)這三個(gè)方面的檢查。如果任何一方面不匹配,Android都不會(huì)將該隱式Intent傳遞給目標(biāo)組件。接下來(lái)我們講解這三方面檢查的具體規(guī)則。
1.動(dòng)作測(cè)試
 
< intent-filter>元素中可以包括子元素< action>,比如:

< intent-filter> 

< action android:name=”com.example.project.SHOW_CURRENT” /> 

< action android:name=”com.example.project.SHOW_RECENT” />

 < action android:name=”com.example.project.SHOW_PENDING” /> 

< /intent-filter> 

一條< intent-filter>元素至少應(yīng)該包含一個(gè)< action>,否則任何Intent請(qǐng)求都不能和該< intent-filter>匹配。如果Intent請(qǐng)求的Action和< intent-filter>中個(gè)某一條< action>匹配,那么該Intent就通過(guò)了這條< intent-filter>的動(dòng)作測(cè)試。如果Intent請(qǐng)求或< intent-filter>中沒(méi)有說(shuō)明具體的Action類型,那么會(huì)出現(xiàn)下面兩種情況。
(1) 如果< intent-filter>中沒(méi)有包含任何Action類型,那么無(wú)論什么Intent請(qǐng)求都無(wú)法和這條< intent- filter>匹配;
(2) 反之,如果Intent請(qǐng)求中沒(méi)有設(shè)定Action類型,那么只要< intent-filter>中包含有Action類型,這個(gè) Intent請(qǐng)求就將順利地通過(guò)< intent-filter>的行為測(cè)試。
2.類別測(cè)試
 
< intent-filter>元素可以包含< category>子元素,比如:

< intent-filter . . . >

 < category android:name=”android.Intent.Category.DEFAULT” />

 < category android:name=”android.Intent.Category.BROWSABLE” /> 

< /intent-filter> 

只有當(dāng)Intent請(qǐng)求中所有的Category與組件中某一個(gè)IntentFilter的< category>完全匹配時(shí),才會(huì)讓該 Intent請(qǐng)求通過(guò)測(cè)試,IntentFilter中多余的< category>聲明并不會(huì)導(dǎo)致匹配失敗。一個(gè)沒(méi)有指定任何類別測(cè)試的 IntentFilter僅僅只會(huì)匹配沒(méi)有設(shè)置類別的Intent請(qǐng)求。
3.數(shù)據(jù)測(cè)試
數(shù)據(jù)在< intent-filter>中的描述如下:  

< intent-filter . . . >

 < data android:type=”video/mpeg” android:scheme=”http” . . . />

 < data android:type=”audio/mpeg” android:scheme=”http” . . . />

 < /intent-filter> 

元素指定了希望接受的Intent請(qǐng)求的數(shù)據(jù)URI和數(shù)據(jù)類型,URI被分成三部分來(lái)進(jìn)行匹配:scheme、 authority和path。其中,用setData()設(shè)定的Inteat請(qǐng)求的URI數(shù)據(jù)類型和scheme必須與IntentFilter中所指定的一致。若IntentFilter中還指定了authority或path,它們也需要相匹配才會(huì)通過(guò)測(cè)試。
4.簡(jiǎn)單例子說(shuō)明
講解完Intent基本概念之后,接下來(lái)我們就使用Intent激活A(yù)ndroid自帶的電話撥號(hào)程序,通過(guò)這個(gè)實(shí)例你會(huì)發(fā)現(xiàn),使用Intent并不像其概念描述得那樣難。最終創(chuàng)建Intent的代碼如下所示。

Intent i = new
Intent(Intent.ACTION_DIAL,Uri.parse(”tel://13800138000″));

創(chuàng)建好Intent之后,你就可以通過(guò)它告訴Android希望啟動(dòng)新的Activity了。

startActivity(i);

Activity啟動(dòng)后顯示界面如下:


三、Intent的構(gòu)造函數(shù)
公共構(gòu)造函數(shù):
1、Intent() 空構(gòu)造函數(shù)
2、Intent(Intent o) 拷貝構(gòu)造函數(shù)
3、Intent(String action) 指定action類型的構(gòu)造函數(shù)
4、Intent(String action, Uri uri) 指定Action類型和Uri的構(gòu)造函數(shù),URI主要是結(jié)合程序之間的數(shù)據(jù)共享ContentProvider
5、Intent(Context packageContext, Class<?> cls) 傳入組件的構(gòu)造函數(shù),也就是上文提到的
6、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前兩種結(jié)合體
Intent有六種構(gòu)造函數(shù),3、4、5是最常用的,并不是其他沒(méi)用!
Intent(String action, Uri uri)  的action就是對(duì)應(yīng)在AndroidMainfest.xml中的action節(jié)點(diǎn)的name屬性值。在Intent類中定義了很多的Action和Category常量。
示例代碼二:

Intent intent = new Intent(Intent.ACTION_EDIT, null);
startActivity(intent);

示例代碼二是用了第四種構(gòu)造函數(shù),只是uri參數(shù)為null。執(zhí)行此代碼的時(shí)候,系統(tǒng)就會(huì)在程序主配置文件AndroidMainfest.xml中尋找<action android:name="android.intent.action.EDIT" />對(duì)應(yīng)的Activity,如果對(duì)應(yīng)為多個(gè)activity具有<action android:name="android.intent.action.EDIT" />此時(shí)就會(huì)彈出一個(gè)dailog選擇Activity。
如果是用示例代碼一那種方式進(jìn)行發(fā)送則不會(huì)有這種情況。
利用Intent在Activity之間傳遞數(shù)據(jù)
在Main中執(zhí)行如下代碼:

 Bundle bundle = new Bundle();
 bundle.putStringArray("NAMEARR", nameArr);
 Intent intent = new Intent(Main.this, CountList.class);
 intent.putExtras(bundle);
 startActivity(intent);

在CountList中,代碼如下:

 Bundle bundle = this.getIntent().getExtras();
 String[] arrName = bundle.getStringArray("NAMEARR");

以上代碼就實(shí)現(xiàn)了Activity之間的數(shù)據(jù)傳遞!


四、常用方法總結(jié)
這篇文章是我剛開始學(xué)習(xí)Android時(shí)看到的,當(dāng)時(shí)理解的不是很深入,現(xiàn)在再回頭看這篇文章總結(jié)的很詳細(xì),在這里與大家分享。
1,調(diào)用web瀏覽器
 

Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com");
returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);

2,地圖

Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

3,調(diào)撥打電話界面
 

Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);

4,直接撥打電話

 
Uri callUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_CALL, callUri);

5,卸載

Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

6,安裝

Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

7,播放

Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);

8,調(diào)用發(fā)郵件

Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

9,發(fā)郵件

returnIt = new Intent(Intent.ACTION_SEND);
String[] tos = { "shenrenkui@gmail.com" };
String[] ccs = { "shenrenkui@gmail.com" };
returnIt.putExtra(Intent.EXTRA_EMAIL, tos);
returnIt.putExtra(Intent.EXTRA_CC, ccs);
returnIt.putExtra(Intent.EXTRA_TEXT, "body");
returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");
returnIt.setType("message/rfc882");
Intent.createChooser(returnIt, "Choose Email Client");

10,發(fā)短信

Uri smsUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.setType("vnd.android-dir/mms-sms");

11,直接發(fā)郵件

Uri smsToUri = Uri.parse("smsto://100861");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "shenrenkui");

12,發(fā)彩信

Uri mmsUri = Uri.parse("content://media/external/images/media/23");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");

用獲取到的Intent直接調(diào)用startActivity(returnIt)就ok了。

五、eg:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:paddingBottom="@dimen/activity_vertical_margin" 
  android:paddingLeft="@dimen/activity_horizontal_margin" 
  android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin" 
  tools:context=".MainActivity" > 
  <Button  
    android:id="@+id/button1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:text="啟動(dòng)第二個(gè)Activity"/> 
 
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
  <TextView  
    android:id="@+id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
 
</LinearLayout> 


package com.android.xiong.intent_one; 
 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class MainActivity extends Activity { 
 
  private Button button1; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     
    button1=(Button)findViewById(R.id.button1); 
    OnClick on=new OnClick(); 
    button1.setOnClickListener(on); 
  } 
   
  class OnClick implements OnClickListener{ 
 
    @Override 
    public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent intent =new Intent(); 
      intent.setClass(MainActivity.this, TwoActivity.class); 
      String [] name={"zhangsan","lis","wangwu"}; 
      int []age={12,13,14}; 
      intent.putExtra("com.android.xiong.intent_one.Name", name); 
      intent.putExtra("com.android.xiong.intent_one.Age", age); 
      startActivity(intent); 
       
       
    } 
     
  } 
 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
  } 
 
} 


package com.android.xiong.intent_one; 
 
import java.util.HashMap; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class TwoActivity extends Activity{ 
  private TextView text1; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_two); 
    text1=(TextView)findViewById(R.id.text1); 
    Intent intent=getIntent(); 
    String []name=intent.getStringArrayExtra("com.android.xiong.intent_one.Name"); 
    int [] age=intent.getIntArrayExtra("com.android.xiong.intent_one.Age"); 
    String print=""; 
    for (int i = 0; i < age.length; i++) { 
      print+="name: "+name[i].toString()+" age: "+age[i]+" \n"; 
    } 
    text1.setText(print); 
  } 
 
} 


相關(guān)文章

最新評(píng)論