您的位置:首页 > 移动开发 > Android开发

第 8 讲 使用Intent和Android应用资源

2014-04-22 23:44 405 查看

第 8 讲 使用Intent和Android应用资源

纲要

l Intent对象详解

l Intent属性及intent-filter配置

l 使用Intent创建Tab页面

l 资源的类型和存储的方式

l 字符串、颜色资源

l 数组(Array)资源

1、Intent对象详解

当一个Activity需要启动另一个Activity时,程序并没有直接告诉系统要启动哪一个Activity,而是通过Intent来表达自己的意图。两个Activity可以把需要交换的数据封装成Bundle对象,然后使用Intent来携带Bundle对象,这样就实现了两个Activity之间的数据交换。

Intent封装Android应用程序需要启动某个组件的”意图”。

下面将对Intent对象进行更全面的介绍。

l 使用Intent启动系统组件

Android的应用程序包含三个重要组件: Activity、Service、BroadcastRecevier,应用程序采用了一致的方式来启动它们---都是依靠Intent来进行启动的, Intent就封装了程序想要启动程序的意图,不仅如此, Intent还可用于与被启动组件交换信息。

使用Intent启动不同组件的方法。

组件类型
启动方法
Activity
startActivity(Intent intent)

startActivityForResult(Intent intent, int requestCode)

Service
ComponentName startService(Intent service)

Boolean bindService(Intent service,ServiceConnection conn,int flags)

BroadcastReceiver

sendBroadcast(Intent intent);
sendBroadcast(Intent intent, String receiverPermission);
sendOrderedBroadcast(Intent intent, String receiverPermission, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras) ;
sendOrderedBroadcast(Intent intent,String receiverPermission )
sendStickyBroadcast(Intent intent);
sendStickyOrderedBroadcast(Intent intent, BroadcastReceiver resultReceiver, Handler scheduler, int initialCode, String initialData, Bundle initialExtras);

2、Intent属性及intent-filter配置

Intent代表了Android应用的启动“意图”,Android应用将会根据Intent来启动指定的组件,至于到底启动哪一个组件,则取决于Intent的各属性。

2.1 Component属性

Intent的Component属性需要接受一个ComponentName对象,ComponentName对象包含如下几个构造器。

ComponentName(String pkg,String cls):创建pkg所在包下的cls类所对应的组件。

ComponentName(Context pkg,String cls):创建pkg所对应包下的cls类所对应的组件。

ComponentName(Contextpkg,Class<?>cls):创建pkg所对应包下的cls类所对应的组件。

以上构造方法说明:创建一个ComponentName需要指定包名和类名。

例1 Component属性的使用(P199)。

strings.xml
<?xml
version="1.0"encoding="utf-8"?>
<resources>

<string
name="app_name">LC8_1</string>

<string
name="action_settings">Settings</string>

<string
name="hello_world">Hello world!</string>

<string
name="btn_m">使用Component启动组件</string>

</resources>

activity_main.xml
<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=".ComponentAttr"
>

<Button
android:id="@+id/btn_m"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_m"
/>

</RelativeLayout>

activity_second.xml
<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=".SecondActivity"
>

<EditText
android:id="@+id/et_s"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</RelativeLayout>

ComponentAttr.java
/**
*

*/
package com.confidant.lc8_1;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
*
@author
xuhaixin
*
*/
publicclass ComponentAttr
extends Activity {

@Override

publicvoid onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.btn_m);

btn.setOnClickListener(new OnClickListener() {

@Override

publicvoid onClick(View v) {

ComponentName comp = new ComponentName(ComponentAttr.this, SecondActivity.class);
// 创建一个ComponentName对象

Intent intent = new Intent();

intent.setComponent(comp); // 为Intent设置Component属性

startActivity(intent);

}

});

}
}

SecondActivity.java
/**
*

*/
package com.confidant.lc8_1;
import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.widget.EditText;
/**
*

*
@author
xuhaixin
*
*/
publicclass SecondActivity
extends Activity {

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

EditText et_s = (EditText) findViewById(R.id.et_s);

ComponentName comp = getIntent().getComponent();//
获取该Activity对应的Intent的Component属性

et_s.setText("组建包名为:" + comp.getPackageName() +
"\n组件类名为:"

+ comp.getClassName());// 显示该ComponentName对象的包名、类名

}

}
AndroidManifest.xml
<?xml
version="1.0"encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.confidant.lc8_1"
android:versionCode="1"
android:versionName="1.0"
>

<uses-sdk

android:minSdkVersion="8"
android:targetSdkVersion="18"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>

<activity
android:name="com.confidant.lc8_1.ComponentAttr"
android:label="@string/app_name"
>

<intent-filter>

<action
android:name="android.intent.action.MAIN"
/>

<category
android:name="android.intent.category.LAUNCHER"
/>

</intent-filter>

</activity>

<activity
android:name="com.confidant.lc8_1.SecondActivity">

</activity>

</application>

</manifest>

2.2 Action、Category属性与intent-filter配置

Intent的Action、Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,而Category则用于为Action增加额外的附加类别信息。通常Action属性会与Category属性结合使用。

Action要完成的只是一个抽象的动作,这个动作具体由哪个组件来完成,Action这个字符串本身并不管,而是取决于Activity的<intent-filter…/>配置,只要某个Activity的<intent-filter…/>配置中包含了该Action所对应的字符串,该Activity就有可能被启动。

例2 Action属性、Category属性的使用(P202)。

strings.xml
<?xml
version="1.0"encoding="utf-8"?>
<resources>

<string
name="app_name">LC8_2</string>

<string
name="action_settings">Settings</string>

<string
name="hello_world">Hello world!</string>

<string
name="btn_m">启动指定Action、指定Category对应的Activity</string>

</resources>

ActionCateAttr.java
/**
*

*/
package com.confidant.lc8_2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
*
@author
xuhaixin
*

*/
publicclass ActionCateAttr
extends Activity {

finalstatic String
XHX_ACTION =
"com.confidant.intent.action.XHX_ACTION";//
定义一个ACTION常量

finalstatic String
XHX_CATEGORY =
"com.confidant.intent.category.XHX_CATEGORY";//
定义一个CATEGORY常量

@Override

publicvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btn_m);

btn.setOnClickListener(new OnClickListener() {

@Override

publicvoid onClick(View v) {

Intent intent = new Intent();

intent.setAction(ActionCateAttr.XHX_ACTION);
// 设置Action属性

intent.addCategory(ActionCateAttr.XHX_CATEGORY);//
添加Category属性

startActivity(intent);

}

});

}
}

SecondActivity.java
/**
*

*/
package com.confidant.lc8_2;

import java.util.Set;

import com.confidant.lc8_2.R;

import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;

/**
*

*
@author
xuhaixin
*

*/
publicclass SecondActivity
extends Activity {

@Override

protectedvoid onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);

EditText et_s = (EditText) findViewById(R.id.et_s);

String action = getIntent().getAction();// 获取该Activity对应的Intent的Action属性

et_s.setText("Action为:" + action);

Set<String> cates = getIntent().getCategories();//
获取该Activity对应的Intent的Category属性

et_s.append("\nCategory属性为:" + cates);//
显示Action属性

}

}

AndroidManifest.xml
<?xml
version="1.0"encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.confidant.lc8_2"
android:versionCode="1"
android:versionName="1.0"
>

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>

<activity
android:name="com.confidant.lc8_2.ActionCateAttr"
android:label="@string/app_name"
>

<intent-filter>

<action
android:name="android.intent.action.MAIN"
/>

<category
android:name="android.intent.category.LAUNCHER"
/>

</intent-filter>

</activity>

<activity
android:name="com.confidant.lc8_2.SecondActivity">

<intent-filter>

<!-- 指定该Activity能响应action为指定字符串的Intent
-->

<action
android:name="com.confidant.intent.action.XHX_ACTION"/>

<!-- 指定该Activity能响应category为指定字符串的Intent
-->

<category
android:name="com.confidant.intent.category.XHX_CATEGORY"/>

<!-- 指定该Activity能响应category为如下字符串的Intent
-->

<category
android:name="android.intent.category.DEFAULT"
/>

</intent-filter>

</activity>

</application>

</manifest>

2.3
指定Action、Category调用系统的Activity

Android内部提供了大量的标准Action、Category常量,其中用于启动Activity的标准Action常量和标准Category常量及所表示的含义如下表

标准Action常量及所表示的含义如下表

Action常量
目标组件
简单说明
ACTION_CALL
Activity
向指定用户打电话

ACTION_EDIT
Activity
编辑指定数据

ACTION_MAIN
Activity
应用程序的入口

ACTION_SYNC
Activity
执行数据同步

ACTION_DIAL
Activity
显示拨号面板

ACTION_SENDTO
Activity
向其他人发送消息

标准Category常量及所表示的含义如下表

Category常量
简单说明
CATEGORY_BROWSABLE
指定该Activity能被浏览器安全调用

CATEGORY_GADGET
该Activity可以被包含在另外一个装载小工具的activity中

CATEGORY_HOME
设置该Activity随系统的启动而运行

CATEGORY_LAUNCHER
Activity显示顶级程序列表中

CATEGORY_PREFERENCE
该Activity是一个选项面板.

CATEGORY_TAB
指定Activity作为TabActivity的Tab页

关于Action、Category属性的常量,请参考教材(P204-205)。

2.4 Data、Type属性与intent-filter配置

Data属性通常用于向Action属性提供操作的数据。Data属性接受一个Uri对象,一个Uri对象通常通过如下形式的字符串来表示。

例: content://com.android.contacts/contacts/1

tel:123

上面所示的两个字符串的冒号前面大致指定了数据的类型,冒号后面的是数据部分。因此一个合法的Uri对象既可以决定操作哪种类型的数据,又可指定具体的数据值。

Type属性则用于明确指定Data属性所指定数据的类型或MIME类型。通常来说,当Intent不指定Data属性时Type属性才会起作用,否则Android系统将会根据Data属性值来分析数据的类型,因此无须指定Type属性。如果一旦为Intent同时指定了Action、Data属性,那么Android将可根据指定的数据类型来启动特定的应用程序。

例3 同时为Intent指定Action、Data属性的例子(P210)。

3、使用Intent创建Tab页面

前面我们学习了使用TabActivity来创建Activity布局,添加Tab页面使用了TabHost.newTabSpec如下方法。

Ø setContent(intviewId):直接将指定View组件设置成Tab页的Content。

Ø setContent(Intentintent):直接将指定Intent对应的Activity设置成Tab页的Content。

4、资源的类型及存储方式

Android应用资源可分为两大类:

Ø 无法直接访问的原生资源,保存在assets目录下。

Ø 可通过R资源清单类访问的资源,保存在res上当下。

4.1
资源的类型及存储方式

Android要求在res目录下用不同的子目录来保存不同的应用资源,不同资源在/res目录下的存储方式,如教材P215所示。

4.2
使用资源

在Android应用中使用资源分为在Java代码和XML文件中使用资源,其中Java代码用于为Android应用定义四大组件,而XML文件中则用于为Android应用定义各种资源。

4.2.1 在Java代码中使用资源
Java代码中访问资源主要是通过R类来完成,其完整的语法格式为:

[<package_name>.]R.<resource_type> .<resource_name>

Ø <package_name>:指定R类所在包。

Ø <resource_type>:R类中代表不同资源类型的子类。

Ø <resource_name >:指定资源的名称。

例如:

//从string资源中获取指定字符串资源,并设置该窗口的标题

getWindow().setTitle(getResources().getText(R.string.main_title));

//获取指定的TextView组件,并设置该组件显示string资源中的指定字符串资源。

TextView msg=(TextView)findViewById(R.id.msg);

msg.setText(R.string.hello_message);

4.2.2 在XML代码中使用资源
当定义XML资源文件时,其中的XML元素可能需要指定不同的值,这些值可设置为已定义的资源项。在XML代码中使用资源的完整的语法格式为:

@[<package_name>:]<resource_type> /<resource_name>

Ø <package_name >:指定资源类所在应用的包。

Ø <resource_type >:R类中代表不同资源类型的子类。

Ø <resource_name >:指定资源的名称。

5、使用字符串、颜色、尺寸资源

字符串资源、颜色资源、尺寸资源,它们对应的XML文件都位于/res/values目录下,它们默认的文件名,以及在R类中对应的内部类如下表所示。

下面通过示例介绍字符串资源、颜色资源和尺寸资源的用法。

5.1
颜色值的定义

颜色值的定义是通过RGB三原色和一个alpha值来定义的。颜色值定义的开始是一个井号(#),后面是Alpha-Red-Green-Blue的格式。例如:

Ø #RGB:分别指定红、绿、蓝三原色的值(0~f)来代表颜色。

Ø #ARGB:分别指定红、绿、蓝三原色的值(0~f)来代表颜色及透明度(0~f)来代表颜色。

Ø #RRGGBB:分别指定红、绿、蓝三原色的值(00~ff)来代表颜色。

Ø #AARRGGBB:分别指定红、绿、蓝三原色的值(00~ff)来代表颜色及透明度(00~ff)来代表颜色。

上面四种形式中,A、R、G、B都代表一个十六进制数年,其中A代表透明度,R代表红色的数值,G代表绿色的数值,B代表蓝色的数值。

5.2
定义资源

字符串资源文件位于/res/values目录下,其根元素是<resource…> ,该元素里每个<string…/>子元素都定义一个字符串常量,其中<string…/>元素中的name属性指定该常量的名称, <string…/>元素开始标签和结束标签之间的内容代表字符串值。

5、使用字符串、颜色、尺寸资源

颜色资源文件位于/res/values目录下,其根元素是<resource…> ,该元素里每个<color…/>子元素都定义一个字符串常量,其中<color…/>元素中的name属性指定该颜色的名称, <color…/>元素开始标签和结束标签之间的内容代表颜色值。

5、使用字符串、颜色、尺寸资源

尺寸资源文件位于/res/values目录下,其根元素是<resource…> ,该元素里每个<dimen…/>子元素定义一个尺寸串常量,其中<dimen…/>元素中的name属性指定该尺寸的名称, <dimen…/>元素开始标签和结束标签之间的内容代表尺寸值。

5.3
使用字符串、颜色、尺寸资源

5、使用字符串、颜色、尺寸资源

6、数组(Array)资源

Android中同推荐在Java程序中定义数组,因为Android允许通过资源文件来定义数组资源。

数组资源文件位于/res/values目录下的arrays.xml文件来定义数组,其根元素是<resource…> ,该元素内可包含如下三种子元素:

Ø <array…/>子元素:定义普通类型的数组。

Ø <string-array…/>子元素:定义字符串数组。

Ø <integer-array…/>子元素:定义整数数组。

一旦在资源文件中定义了数组资源,接下来就可以在Java文件中通过如下形式来访问资源了:

[<package_name>.]R.array.array_name

为了能在Java程序访问到实际数组,Resources提供如下方法。

Ø String[]getStringArray(int id):根据资源文件中字符串数组资源的名称来获取实际的字符串数组。

Ø Int[]getIntArray(int id):根据资源文件中整型数组资源的名称来获取实际的整型数组。

Ø TypedArrayobtain TypedArray(int id) :根据资源文件中普通数组资源的名称来获取实际的普通数组。

例6 数组资源的使用(P222)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: