Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10037
  • 博文数量: 9
  • 博客积分: 251
  • 博客等级: 二等列兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-19 23:01
文章分类
文章存档

2012年(9)

我的朋友
最近访客

分类: 嵌入式

2012-08-24 09:06:18

2012.08.20 夜
android开发现在也有一个多月了,但是现在自己好像还是啥也不懂。主要原因是自己的基础比较差,正儿八经的嵌入式项目也没有参加过。
写这博客的目的一是检讨自己;二来整理一下自己的所学的东西。根据以前的经验来看,学懂一样东西挺容易但是要想讲懂的话比较困难。
一个android应用程序的构造思想为模块化单元化,总的说来一个android应用程序就是几个activity的集合,但是这几个Activity之间是如何进行串联组合成一个正儿八经能用的程序的呢,就是通过intent来实现。
intent负责对应用中一次操作的动作,动作的数据以及动作涉及数据,附加数据进行描述。android则根据此intent的描述,负责找到对应的组件,并将Intent传递给对应的组件,完成组件的调用。也就是说intent在这里起到一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
理解Intent的关键之一就是理解清楚Intent的两种基本用法:一种是显式的Intent,在构造Intent对象的时候就指定接受者;另一种是隐式的Intent,Intent的发送者在构造Intent对象时,并不知道也不关心接受者是谁,在使用的时候动态绑定。
(1) 关于隐式Intent的解析
Intent的解析机制主要通过查找已经注册在AndroidManifest.xml中的所有IntentFilter及其中定义的Intent,最终找到匹配的Intent。这个解析过程中,android主要是通过Intentaction/type/category这三个属性来判断的。

一 Intent的属性设置:
(1) Action
所要执行的动作,这个动作是抽象的,动作的具体完成由组件完成。
也可以自定义动作,并定义相应的Activity来处理自定义动作。
(2) category 类别
通常Action属性要和Category属性结合使用;
实例:
main.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:gravity="center_horizontal"
  7.     >
  8. <Button
  9.     android:id="@+id/bn"
  10.     android:layout_width="wrap_content"
  11.     android:layout_height="wrap_content"
  12.     android:text="启动指定Action、默认Category对应的Activity"
  13.     />
  14. </LinearLayout>
second.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:gravity="center_horizontal"
  7.     >
  8. <EditText
  9.     android:id="@+id/show"
  10.     android:layout_width="fill_parent"
  11.     android:layout_height="wrap_content"
  12.     android:text="第二个Activity"
  13.     android:editable="false"
  14.     android:cursorVisible="false"
  15.     />
  16. </LinearLayout>
AndroidManifest.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.      package="org.crazyit.intent"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <activity android:name=".ActionAttr"
  8.                  android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.MAIN" />
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </activity>
  14.         <activity android:name=".SecondActivity"
  15.                  android:label="@string/app_name">
  16.             <intent-filter>
  17.                 <!-- 指定该Activity能响应Action为指定字符串的Intent -->
  18.                 <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
  19.                 <!-- 指定该Action能响应Category属性为指定字符串的Intent -->
  20.                 <category android:name="android.intent.category.DEFAULT" />
  21.             </intent-filter>
  22.         </activity>
  23.     </application>


  24. </manifest>
元素用于配置该Activity所能相应的Intent。
元素里包含三种元素:
子元素
子元素
子元素
里包含多个子元素时,表明该Activity能响应action属性值为其中任意一个字符串的Intent。
注意:
·一个Intent对象最多只能包括一个Action属性,程序可以调用Intent的setAction(String str)方法来设置Action属性值;
·一个Intent对象可以包含多个Category属性,程序默认的属性值为Intent.CATEGORY_DEFAULT常量。

ActionAttr.java

点击(此处)折叠或打开

  1. package org.crazyit.intent;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;

  8. public class ActionAttr extends Activity
  9. {
  10.     public final static String CRAZYIT_ACTION
  11.         = "org.crazyit.intent.action.CRAZYIT_ACTION";
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState)
  14.     {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.main);
  17.         Button bn = (Button)findViewById(R.id.bn);
  18.         //为bn按钮绑定事件监听器
  19.         bn.setOnClickListener(new OnClickListener()
  20.         {
  21.             public void onClick(View arg0)
  22.             {
  23.                 //创建Intent对象
  24.                 Intent intent = new Intent();
  25.                 //为Intent设置Action属性(属性值就是一个普通字符串)
  26.                 intent.setAction(ActionAttr.CRAZYIT_ACTION);
  27.                 startActivity(intent);
  28.             }
  29.         });
  30.     }
  31. }

SecondActivity.java

点击(此处)折叠或打开

  1. package org.crazyit.intent;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.EditText;

  5. public class SecondActivity extends Activity
  6. {
  7.     @Override
  8.     public void onCreate(Bundle savedInstanceState)
  9.     {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.second);
  12.         EditText show = (EditText)findViewById(R.id.show);
  13.         //获取该Activity对应的Intent的Action属性
  14.         String action = getIntent().getAction();
  15.         //显示Action属性
  16.         show.setText("Action为:" + action);
  17.     }
  18. }
(3) Data 执行动作所要操作的数据(采用指向数据的一个URI来表示)
ex: content://com.android.example/contacts/1
冒号前面大致指定了数据的类型,后面是数据的部分;
(4) type 数据类型
用于指定Data属性所指定的数据类型或MIME类型
(5) component 组件
指定组件的类名称,通常android会根据Intent中包含的其他属性信息,最终找到一个与之匹配的目标组件。但如果指定了这个属性以后,Intent的其他属性都是可选的。
Component组件需要接受一个ComponentName对象,ComponentName对象包括下面几个构造器:
ComponentName(String pkg, String cls) :创建pkg所对应的包下的cls类所对应的组件 ComponentName(Context pkg, String cls) :………… 
ComponentName(Context pkg, Class cls):…………
以上三个函数的本质就是创建一个ComponentName需要指定特定的包名和类名,这就可以唯一的确定一个组件类,这样应用程序就可以根据给定的组件类去启动特定的组件。

实例:通过Intent的Component启动Activity

ComponentTest.java

点击(此处)折叠或打开

  1. public class ComponentTest extends Activity
  2. {
  3.     @Override
  4.     public void onCreate(Bundle savedInstanceState)
  5.     {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         Button bn = (Button)findViewById(R.id.bn);
  9.         //为bn按钮绑定事件监听器
  10.         bn.setOnClickListener(new OnClickListener()
  11.         {
  12.             public void onClick(View arg0)
  13.             {
  14.                 ComponentName comp = new ComponentName(ComponentTest.this,
  15. SecondActivity.class);
  16.                 Intent intent = new Intent();
  17.                 intent.setComponent(comp);
  18.                 startActivity(intent);
  19.             }            
  20.         });
  21.     }
  22. }
上边三行斜体红色代码创建了一个ComponentName对象,并将对象设置成Intent对象的Component属性。
这三行代码可以简化为
Intent intent = new Intent(ComponentAttr.this, SecondActivity.class);

SecondActivity.java

点击(此处)折叠或打开

  1. public class SecondActivity extends Activity
  2. {
  3.     public void onCreate(Bundle savedInstanceState)
  4.     {
  5.         super.onCreate(savedInstanceState);
  6.         setContentView(R.layout.second);
  7.         EditText show = (EditText)findViewById(R.id.show);
  8.         ComponentName comp = getIntent().getComponent();
  9.         show.setText("组件包名为:" + comp.getPackageName()
  10.             + "\n组件类名为:" + comp.getClassName());
  11.     }
  12. }
总结:
通过intent的Component启动特性属性时,被启动的组件几乎用进行设置。
(6) extras 附加信息
使用intent可以为组件提供扩展信息
三 用Intent激活一个拨号程序
     新建一个project在activity_main.xml中添加一个button按钮

点击(此处)折叠或打开

  1. <LinearLayout xmlns:android=""
  2.      xmlns:tools=""
  3.      android:layout_width="match_parent"
  4.      android:layout_height="match_parent" >
  5.      <Button
  6.         android:id="@ id/button_id"
  7.         android:layout_width="fill_parent"
  8.         android:layout_height="wrap_content"
  9.         android:text="@string/button_text"
  10.         ></Button>
  11.     </LinearLayout>
Activity的代码:

点击(此处)折叠或打开

  1. package com.example.test;

  2. import android.net.Uri;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.view.View;
  7. import android.widget.Button;

  8. public class MainActivity extends Activity {

  9.     @Override
  10.     public void onCreate(Bundle savedInstanceState) {
  11.         super.onCreate(savedInstanceState);
  12.         setContentView(R.layout.activity_main);
  13.         
  14.         final Button button_test = (Button) findViewById(R.id.button_id);
  15.         button_test.setOnClickListener(new Button.OnClickListener() {

  16.             public void onClick(View arg0) {
  17.                 // TODO Auto-generated method stub
  18.                 Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel://13261998726"));
  19.                 startActivity(i);
  20.             }
  21.         });
  22.     }
  23. }

编译运行,当点击button时进入拨号页面。
阅读(926) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~