Intent詳解

   咱們已經知道了Android的基本組件了,今天講的Intent就是鏈接他們之間的橋樑,確切的說是對Activity,Service,BroadcastReceiver的激活並做爲他們之間的鏈接器。
java

   總的來講Intent的功能有如下三點:
android

(1)激活組件,前面提到的三個基本組件的激活,可是激活方式不一樣;服務器

(2)使用Android系統內置的Intent來完成發送短信、撥打電話,瀏覽網頁和查看地圖等手持設備的基本功能;網絡

(3)激活組件時在組件之間傳遞數據;app

   

   下面咱們將詳細的介紹Intent的相關知識和使用。
ide

一.Intent對象的屬性this

整理爲下列表格中:.net

屬性字段 名稱 數據類型 獲取方法 設置方法
Component Name 組件名 ComponentName getComponent()

setComponent()3d

setClass()xml

setClassName()

Action 動做 String getAction() setAction()
Data 數據 URI

getData()

getType()

setData()

setType()

setDataAndType()

Category 分類 String getCategories()

addCategory()

removeCategpry()

Extra 額外信息

1.鍵值對形式

<String,Object>

2.Bundle類型

1.get***Extra():

獲取不一樣的數據類型

2.getExtras():

獲取封裝好的Bundle數據包

1.putExtra():

以鍵值對的形式存放各類類型的數據

2.putExtras():

放入包裝好的Bundle數據包

Flag 標誌 Integer getFlags() setFlags()

下面具體講解每個屬性:

1.Component Name:


——組件名用來處理Intent的匹配問題,它能夠被設置也能夠不被設置:

(1)設置了組件名的Intent爲顯示的Intent,直接使用組件名裏的參數而實現鏈接器的功能,在開發過程當中經常使用於一個應用程序內部的組件激活;

(2)未設置組件名的Intent爲隱式的Intent,則將會從Action、Data(URI和Type)或者Category中尋找匹配信息,在開發中經常使用於激活其餘應用程序的組件;


——用顯示的方式指定Intent的匹配信息的方法有以下四種:

(1)直接在Intent的定義中指定組件名(最經常使用):

Intent i=new Intent(MainActivity.this,Act2.class);
       startActivity(i);

(2)使用setComponent()方法:

Intent i=new Intent();
       ComponentName cn=new ComponentName(MainActivity.this, Act2.class);
       i.setComponent(cn);
       startActivity(i);

(3)使用setClass()方法:

Intent i=new Intent();
       i.setClass(MainActivity.this, Act2.class);
       startActivity(i);

(4)使用setClassName()方法:

Intent i=new Intent();
    i.setClassName(getBaseContext(), Act2.class.getName());
    startActivity(i);

——用隱式的方式指定Intent的匹配信息,就要用到IntentFilter(Intent過濾器)了,經過在IntentFilter中的Action、URI、Category屬性配置,先放一放後面再說。


2.Action:

   用於描述Intent將要完成什麼動做,分爲系統自帶的和本身定義的兩種:

(1)經常使用的Android內置的Action列入下表格中:

   Activity組件的:

Action的值 完成的動做
ACTION_CALL 直接撥打Data屬性指定的電話號碼
ACTION_EDIT 給用戶顯示可編輯的數據
ACTION_MAIN 開啓一個Activity並做爲整個應用程序的入口Activity
ACTION_SYNC 同步服務器上的數據和移動設備上的數據
ACTION_DIAL 將Data中的電話號碼放入撥打電話的界面中顯示給用戶,用戶能夠手動撥出
ACTION_VIEW 使用與Data中的數據相對應的應用程序來顯示Data中指定的數據
ACTION_SEND 發送數據
ACTION_SENDTO 使用與Data中的數據相對應的應用程序向Data中指定的地址發送數據

   BroadcastReceiver組件的:

Action的值 完成的動做
ACTION_BATTRY_LOW 一個電池電量低的警告
ACTION_TIMEZONE_CHANGED 時區改變的廣播
ACTION_SCREEN_ON 屏幕被打開

(2)自定義Action之後講到廣播時在進行詳細講解

(3)Action使用的例子,一會先說完Data以後一塊兒使用。


3.Data:

(1)Data屬性包括兩個部分:URI和Type

(2)Data的經常使用取值以下:

Data取值 說明

file:///

後接本地文件數據的路徑
mailto:// 後接電子郵件地址
geo:// 地理位置信息,後接經緯度
smsto:// 發短信的電話號碼
tel:// 打電話的電話號碼
content:// 後接內容的定位
http:// 超文本,後接網絡資源的URI

(3)到目前爲止咱們該拿出示例代碼了:

   MainActivity.java

package com.example.l0824_intent_actionanddata;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
    private Button btn_call,btn_send,btn_http,btn_file;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_call=(Button)findViewById(R.id.button_call);
        btn_send=(Button)findViewById(R.id.button_send);
        btn_http=(Button)findViewById(R.id.button_http);
        btn_file=(Button)findViewById(R.id.button_file);
        btn_call.setOnClickListener(this);
        btn_send.setOnClickListener(this);
        btn_http.setOnClickListener(this);
        btn_file.setOnClickListener(this);
                                                                                                                                                                                                                                                                             
    }
    @Override
    public void onClick(View v) {
        Intent i=null;
        switch (v.getId()) {
        case R.id.button_call:
            i=new Intent(Intent.ACTION_CALL,Uri.parse("tel://10086"));
            break;
        case R.id.button_send:
            i=new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto://10086"));
            break;
        case R.id.button_http:
            i=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
            break;
        case R.id.button_file:
            i=new Intent(Intent.ACTION_VIEW);
            i.setDataAndType(Uri.parse("file:///mnt/sdcard/a.mp3"), "audio/mp3");
            break;
        default:
            break;
        }
        startActivity(i);
                                                                                                                                                                                                                                                                             
    }
                                                                                                                                                                                                                                                                         
}

   AndroidManiFest.xml

//添加打電話的權限
<uses-permission android:name="android.permission.CALL_PHONE"/>

   main_activity.xml

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button_call"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="打電話" />
    <Button
        android:id="@+id/button_send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="發短信" />
    <Button
        android:id="@+id/button_http"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="瀏覽網頁" />
    <Button
        android:id="@+id/button_file"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="播放影音" />
</LinearLayout>

   運行效果:


4.Category:

經常使用的Category列以下表:

CATEGORY_HOME 顯示Home界面的Activity
CATEGORY_LAUNCHER 用於一個應用程序的入口Activity
CATEGOYR_DEFAULT 隱式Intent用到

隱式Intent的例子:

前面講過隱式Intent多用於不一樣的應用程序之間,那咱們下面就建立兩個應用程序來講明:

(1)MainActivity.java

package com.example.l0824_intent_default;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
                                                                                                               
            @Override
            public void onClick(View arg0) {
                Intent i=new Intent("com.example.l0824_intent_default.intent.action.Act_Default");
                startActivity(i);
            }
        });
    }
                                                                                                       
}

(2)Act_Default.java

package com.example.l0824_intent_default;
import android.app.Activity;
import android.os.Bundle;
public class Act_Default extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_default);
    }
}

(3)ActivityMainfest.xml

<activity
          android:name="com.example.l0824_intent_default.Act_Default">
          <action android:name="com.example.l0824_intent_default.intent.action.Act_Default" />
           <category android:name="android.intent.category.DEFAULT" />
      </activity>


5.Extra(額外信息)和Intent數據傳遞

有一下兩種傳遞方式:

(1)直接以鍵值對的形式傳遞

   發送方:

Intent i=new Intent(MainActivity.this,Act2.class);
       i.putExtra("name", "張菲");
       i.putExtra("password", "123");
       startActivity(i);

   接收方:

String name=getIntent().getStringExtra("name");
String password=getIntent().getStringExtra("password");

(2)使用Bundle數據包裝數據

   發送方:

Intent i=new Intent(MainActivity.this,Act2.class);
       Bundle b=new Bundle();
       b.putString("name", "張菲");
       b.putInt("password", 123);
       i.putExtras(b);
       startActivity(i);

   接收方:

Bundle b=getIntent().getExtras();
        String name =b.getString("name");
        int pass=b.getInt("password");


6.flag(標誌)

   用的不多,幾乎不用。

相關文章
相關標籤/搜索