最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中的Intent對象完全解析

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

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

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

下面是一些簡單的例子:

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

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

三、 intent的解析
應用程序的組件為了告訴Android自己能響應、處理哪些隱式Intent請求,可以聲明一個甚至多個Intent Filter。每個Intent Filter描述該組件所能響應Intent請求的能力——組件希望接收什么類型的請求行為,什么類型的請求數(shù)據(jù)。比如之前請求網(wǎng)頁瀏覽器這個例子中,網(wǎng)頁瀏覽器程序的Intent Filter就應該聲明它所希望接收的Intent Action是WEB_SEARCH_ACTION,以及與之相關的請求數(shù)據(jù)是網(wǎng)頁地址URI格式。如何為組件聲明自己的Intent Filter? 常見的方法是在AndroidManifest.xml文件中用屬性< Intent-Filter>描述組件的Intent Filter。
前面我們提到,隱式Intent(Explicit Intents)和Intent Filter(Implicit Intents)進行比較時的三要素是Intent的動作、數(shù)據(jù)以及類別。實際上,一個隱式Intent請求要能夠傳遞給目標組件,必要通過這三個方面的檢查。如果任何一方面不匹配,Android都不會將該隱式Intent傳遞給目標組件。接下來我們講解這三方面檢查的具體規(guī)則。
1.動作測試
 
< 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>元素至少應該包含一個< action>,否則任何Intent請求都不能和該< intent-filter>匹配。如果Intent請求的Action和< intent-filter>中個某一條< action>匹配,那么該Intent就通過了這條< intent-filter>的動作測試。如果Intent請求或< intent-filter>中沒有說明具體的Action類型,那么會出現(xiàn)下面兩種情況。
(1) 如果< intent-filter>中沒有包含任何Action類型,那么無論什么Intent請求都無法和這條< intent- filter>匹配;
(2) 反之,如果Intent請求中沒有設定Action類型,那么只要< intent-filter>中包含有Action類型,這個 Intent請求就將順利地通過< intent-filter>的行為測試。
2.類別測試
 
< intent-filter>元素可以包含< category>子元素,比如:

< intent-filter . . . >

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

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

< /intent-filter> 

只有當Intent請求中所有的Category與組件中某一個IntentFilter的< category>完全匹配時,才會讓該 Intent請求通過測試,IntentFilter中多余的< category>聲明并不會導致匹配失敗。一個沒有指定任何類別測試的 IntentFilter僅僅只會匹配沒有設置類別的Intent請求。
3.數(shù)據(jù)測試
數(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請求的數(shù)據(jù)URI和數(shù)據(jù)類型,URI被分成三部分來進行匹配:scheme、 authority和path。其中,用setData()設定的Inteat請求的URI數(shù)據(jù)類型和scheme必須與IntentFilter中所指定的一致。若IntentFilter中還指定了authority或path,它們也需要相匹配才會通過測試。
4.簡單例子說明
講解完Intent基本概念之后,接下來我們就使用Intent激活Android自帶的電話撥號程序,通過這個實例你會發(fā)現(xiàn),使用Intent并不像其概念描述得那樣難。最終創(chuàng)建Intent的代碼如下所示。

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

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

startActivity(i);

Activity啟動后顯示界面如下:


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

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

示例代碼二是用了第四種構造函數(shù),只是uri參數(shù)為null。執(zhí)行此代碼的時候,系統(tǒng)就會在程序主配置文件AndroidMainfest.xml中尋找<action android:name="android.intent.action.EDIT" />對應的Activity,如果對應為多個activity具有<action android:name="android.intent.action.EDIT" />此時就會彈出一個dailog選擇Activity。
如果是用示例代碼一那種方式進行發(fā)送則不會有這種情況。
利用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");

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


四、常用方法總結
這篇文章是我剛開始學習Android時看到的,當時理解的不是很深入,現(xiàn)在再回頭看這篇文章總結的很詳細,在這里與大家分享。
1,調用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,調撥打電話界面
 

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,調用發(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直接調用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="啟動第二個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); 
  } 
 
} 


相關文章

  • Android編程之界面跳動提示動畫效果實現(xiàn)方法

    Android編程之界面跳動提示動畫效果實現(xiàn)方法

    這篇文章主要介紹了Android編程之界面跳動提示動畫效果實現(xiàn)方法,實例分析了Android動畫效果的布局及功能相關實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • Android多線程AsyncTask詳解

    Android多線程AsyncTask詳解

    這篇文章主要為大家詳細介紹了Android多線程AsyncTask的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 解決Kotlin 類在實現(xiàn)多個接口,覆寫多個接口中相同方法沖突的問題

    解決Kotlin 類在實現(xiàn)多個接口,覆寫多個接口中相同方法沖突的問題

    這篇文章主要介紹了解決Kotlin 類在實現(xiàn)多個接口,覆寫多個接口中相同方法沖突的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 手機方向傳感器的缺點及解決方法探究

    手機方向傳感器的缺點及解決方法探究

    今天小編就為大家分享一篇關于手機方向傳感器的缺點及解決方法探究,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android開發(fā)中播放聲音的兩種方法分析

    Android開發(fā)中播放聲音的兩種方法分析

    這篇文章主要介紹了Android開發(fā)中播放聲音的兩種方法,結合實例形式簡單分析了Android音頻播放的常用函數(shù)、使用方法及相關注意事項,需要的朋友可以參考下
    2017-09-09
  • Android Handler消息機制分析

    Android Handler消息機制分析

    這篇文章主要介紹了Android Handler消息機制分析,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • Android 如何獲取設備唯一標識

    Android 如何獲取設備唯一標識

    這篇文章主要介紹了Android 如何獲取設備唯一標識,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下
    2021-03-03
  • Qt6.5.3?Android環(huán)境配置的實現(xiàn)

    Qt6.5.3?Android環(huán)境配置的實現(xiàn)

    本文主要介紹了Qt6.5.3?Android環(huán)境配置的實現(xiàn),文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-01-01
  • Android PC投屏功能實現(xiàn)的示例代碼

    Android PC投屏功能實現(xiàn)的示例代碼

    本篇文章主要介紹了Android PC投屏功能實現(xiàn)的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • 深入淺析Android消息機制

    深入淺析Android消息機制

    在Android中,線程內部或者線程之間進行信息交互時經(jīng)常會使用消息,這些基礎的東西如果我們熟悉其內部的原理,將會使我們容易、更好地架構系統(tǒng),避免一些低級的錯誤,通過本文給大家介紹android消息機制,感興趣的朋友一起學習吧
    2016-04-04

最新評論

双辽市| 布尔津县| 阿合奇县| 乐平市| 霍城县| 濮阳县| 樟树市| 富蕴县| 鄂伦春自治旗| 四会市| 新闻| 周至县| 瑞金市| 治县。| 晋江市| 府谷县| 偏关县| 进贤县| 星子县| 绍兴县| 龙泉市| 综艺| 咸丰县| 新巴尔虎左旗| 涟水县| 万全县| 攀枝花市| 浑源县| 关岭| 双牌县| 观塘区| 辽阳市| 始兴县| 明水县| 微山县| 鹿泉市| 大埔区| 柏乡县| 马山县| 砚山县| 中方县|