自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android的Intent機(jī)制實(shí)例講解

移動(dòng)開發(fā) Android
Intent是Android中簡單的消息傳遞框架。使用Intent,你可以在整個(gè)系統(tǒng)內(nèi)廣播消息或者給特定的Activity或者服務(wù)來執(zhí)行你的行為意圖。系統(tǒng)會(huì)決定那個(gè)(些)目標(biāo)來執(zhí)行適當(dāng)?shù)男袨椤1疚挠脤?shí)例介紹了Intent的用法。

在Android應(yīng)用程序框架中,有一個(gè)比較強(qiáng)大的事件處理機(jī)制——Intent(意圖)。Intent(意圖)的作用與事件(event)很像,但與傳統(tǒng)的事件處理有些差異。

傳統(tǒng)的事件處理,講究的是處理者(handler)的觸發(fā),當(dāng)事件發(fā)生時(shí),便callback事件的處理者,或是直接將該事件傳送(forward)給應(yīng)用程序,由應(yīng)用程序決定處理方式。

在「Intent」這樣的事件處理觀念里,Android 試圖將事件解釋為「應(yīng)用程序的意圖」或是「使用者的意圖」,并試著去解釋該意圖的目的,若 Android 系統(tǒng)本身能理解應(yīng)用程序的意圖,便會(huì)自行去處理該意圖所應(yīng)執(zhí)行的工作。Android的做法是,在每一意圖(Intent)都帶有一個(gè)動(dòng)作 (action),并根據(jù)不同的動(dòng)作去行動(dòng)。

下面是一個(gè)通過Intent實(shí)現(xiàn)一個(gè)自動(dòng)撥號的例子:

建立一個(gè)android工程IntentDialer,編輯IntentDialer.java:

  1. package com.android;   
  2.    
  3. import android.app.Activity;   
  4. import android.content.Intent;   
  5. import android.net.Uri;   
  6. import android.os.Bundle;   
  7. import android.widget.Toast;   
  8.    
  9. public class IntentDialerActivity extends Activity {   
  10.     /** Called when the activity is first created. */   
  11.     @Override   
  12.     public void onCreate(Bundle savedInstanceState) {   
  13.         super.onCreate(savedInstanceState);   
  14.         setContentView(R.layout.main);   
  15.            
  16.         Intent dial = new Intent();   
  17.         dial.setAction("android.intent.action.CALL");   
  18.         dial.setData(Uri.parse("tel:13428720000"));   
  19.         startActivity(dial);   
  20.         Toast.makeText(this"calling to young 13428720000", Toast.LENGTH_LONG).show();   
  21.     }   
  22. }   

因?yàn)閜ermission的關(guān)系,所以也要在AndroidManifest.xml里加上「CALL_PHONE」的權(quán)限。編輯AndroidManifest.xml:

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     package="com.android"   
  4.     android:versionCode="1"   
  5.     android:versionName="1.0" >   
  6.    
  7.     <uses-sdk android:minSdkVersion="10" />   
  8.    
  9.     <application   
  10.         android:icon="@drawable/ic_launcher"   
  11.         android:label="@string/app_name" >   
  12.         <activity   
  13.             android:name=".IntentDialerActivity"   
  14.             android:label="@string/app_name" >   
  15.             <intent-filter>   
  16.                 <action android:name="android.intent.action.MAIN" />   
  17.    
  18.                 <category android:name="android.intent.category.LAUNCHER" />   
  19.             </intent-filter>   
  20.         </activity>   
  21.     </application>   
  22.     <uses-permission android:name="android.permission.CALL_PHONE" />   
  23.        
  24.    
  25. </manifest>   

這個(gè)例子相當(dāng)簡單,但足以說明Intent的核心了。代碼說明:

先新建一個(gè)Intent對象:

  1. Intent dial = new Intent(); 

設(shè)定Intent的action為「android.intent.action.CALL」,這是一個(gè)內(nèi)建的action:

  1. dial.setAction("android.intent.action.CALL"); 

內(nèi)建action「CALL」需要附帶一筆資料,而資料的寫法是使用URI格式:

  1. dial.setData(Uri.parse("tel:XXXXX")); 

「CALL」是一個(gè)activity action,所以調(diào)用startActivity()將Intent送給框架:

  1. startActivity(dial);  

這個(gè)例子的概念并不難理解:送出一個(gè)帶有內(nèi)建action的Intent給框架,因?yàn)閍ction為CALL,所以框架會(huì)去啟動(dòng)撥號activity并撥打電話。

程序運(yùn)行結(jié)果:

責(zé)任編輯:徐川 來源: Linux社區(qū)
相關(guān)推薦

2014-07-15 10:16:02

AndroidIntent

2013-05-27 14:06:14

Android開發(fā)移動(dòng)開發(fā)Intent機(jī)制

2013-03-28 09:07:37

Android開發(fā)Intent機(jī)制

2013-01-10 15:36:44

Android開發(fā)組件Intent

2009-04-03 08:21:37

AndroidGoogle移動(dòng)OS

2010-01-25 16:52:22

Android Int

2009-06-17 13:57:54

java實(shí)例Reflection

2011-05-30 14:00:35

Android Activity Intent

2011-04-01 09:04:09

RIP

2011-05-23 13:24:01

2009-11-23 17:56:44

PHP緩存機(jī)制

2009-11-23 17:31:49

PHP時(shí)間戳

2009-11-23 20:16:17

PHP接口特性

2011-04-07 13:09:03

明文驗(yàn)證

2010-06-03 18:22:38

Hadoop

2010-09-14 17:20:57

2011-04-02 16:37:26

PAT

2017-02-21 12:20:20

Android事件分發(fā)機(jī)制實(shí)例解析

2013-05-21 09:56:15

2009-04-03 08:26:02

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號