Intent Broadcast Receiver l Intent l Broadcast Receiver
第五章 Intent与 Broadcast. Receiver
本章重点 l 精通Intent原理及使用 l 熟练Broadcast. Receiver l 精通Handler消息传递 l 熟悉Async. Task类
5. 1. 1 Intent原理及分类 l Intent是Android应用内不同组件之间的通讯载体 l 使用Intent可以激活Android的三个核心组件: n Activity n Service n Broadcast. Receiver
l Intent启动Activity、Service和Broadcast. Receiverd的方法: n 启动Activity:调用start. Activity(Intent intent)或 start. Activity. For. Result(Intent intent, int request. Code)方法 n 启动Service:调用start. Service(Intent intent)或bind. Service(Intent intent, Service. Connection conn , int flags)方法 n 触发Broadcast. Receiver时,调用send. Broadcast(Intent intent)方法
l Intent分为以下两类: n 显式Intent n 隐式Intent
5. 1. 2 Intent属性 l Intent对象中包含以下几种属性: n Component组件 n Action动作 n Category类别 n Data数据 n Type数据类型 n Extras扩展信息 n Flags标志位
Component组件 l Component组件为目标组件,需要接受一个Component. Name对象 l Component. Name对象的构造方法有以下几种方式: n Component. Name(String pkg, String class. Name) n Component. Name(Context context, Class<? > class. Name) l Intent还可以指定待启动组件的包名和类名: n set. Class(Context ctx, Class<? > cls) n set. Class. Name(Context ctx, String class. Name) n set. Class. Name(String pkg, String class. Name)
l 创建Component. Name对象 Intent intent =new Intent(); Component. Name component = new Component. Name(Main. Activity. this , Second. Activity. class); intent. set. Component(component); start. Activity(intent); l 使用set. Class()方法指定待启动组件 Intent intent = new Intent(); intent. set. Class(Main. Activity. this, Second. Activity. class); start. Activity(intent); Context对象 Class对 象 l 使用Intent()构造方法指定启动组件 Intent intent = new Intent(Main. Activity. this, Second. Activity. class); start. Activity(intent);
Action动作 l Action是一个字符串,用于描述一个Android应用程序的组件 l 启动Activity的系统标准Action常量 字符串 ACTION_SEND ACTION_MAIN android. intent. action. SEND android. intent. action. MAIN ACTION_SENDTO ACTION_VIEW android. intent. action. SENDTO android. intent. action. VIEW ACTION_ANSWER ACTION_EDIT android. intent. action. ANSWER android. intent. action. EDIT ACTION_INSERT ACTION_PICK android. intent. action. INSERT android. intent. action. PICK ACTION_DELETE ACTION_DIAL android. intent. action. DELETE android. intent. action. DIAL ACTION_ALL_APPS ACTION_CALL android. intent. action. ACTION_ALL_APPS android. intent. action. CALL ACTION_SEARCH android. intent. action. SEARCH 描述 描述 启动一个Activity,该Activity会发送Intent中指定 应用程序入口 的数据。接收人需要由解析的Activity来选择。 最常见的动作;视图要求以最合理的方式查看 启动一个Activity来向Intent的数据URI所指定的联 Intent的URI中所提供的数据。不同的应用程序 系人发送一条消息。 将会根据URI模式来处理视图请求。 打开一个处理来电的Activity,通常这个动作是由 请求一个Activity,要求该Activity可以编辑 本地电话拨号程序处理 Intent的数据URI中的数据 打开一个子Activity能在Intent的数据URI指定的游 启动一个子Activity,可以从Intent的数据URI指 标处插入新项的Activity。当作为子Activity调用时, 定的Content. Provider中选择一个项。当关闭的时 应该返回一个指向新插入项的URI 候,返回所选择的项的URI。 启动一个Activity,允许删除Intent的数据URI中指 打开一个拨号程序,要拨打的号码由Intent的数 定的数据 据URI预先提供。 打开一个列出所有已安装应用程序的Activity,通 打开一个电话拨号程序,并立即使用Intent的数 常此操作由启动器处理 据URI所提供的号码拨打一个电话,此动作只应 用于代替本地电话的Activity 通常用于启动特定的搜索Activity。
l Android中预先定义了一些标准Action,值和意义总结如下: n ACTION_BOOT_COMPLETED:系统启动完成广播 n ACTION_TIME_CHANGED:时间改变广播 n ACTION_DATE_CHANGED:日期改变广播 n ACTION_TIME_TICK:每分钟改变一次时间 n ACTION_TIMEZONE_CHANGED:时区改变广播 n ACTION_BATTERY_LOW:电量低广播 n ACTION_PACKAGE_ADDED:添加包广播 n ACTION_PACKAGE_REMOVED:删除包广播
Category类别 l Category属性用来描述动作的类别 Category常量 字符串 描述 CATEGORY_DEFAULT android. intent. category. DEFAULT 默认的Category CATEGORY_BROWSABLE android. intent. category. BROWSABLE 指定Activity能被浏览器安全调用 CATEGORY_TAB android. intent. category. TAB 指定Activity能作为Tab. Activity的Tab页 CATEGORY_LAUNCHER android. intent. category. LAUNCHER Activity显示顶级程序列表中 CATEGORY_INFO android. intent. category. INFO 用于提供包信息 CATEGORY_HOME android. intent. category. HOME 设置该Activity随系统启动而运行 CATEGORY_PREFERENCE android. intent. category. PREFERENCE 该Activity是参数面板 CATEGORY_TEST android. intent. category. TEST 该Activity是一个测试 指定手机被插入汽车底座时运行该 Activity 讲师演示讲解 指定手机被插入桌面底座时运行该 CATEGORY_DESK_DOCK android. intent. category. CAR_DOCK Activity 【代码5 - 1】activity_home. xml【代码5 - 2】Home. Activity. java CATEGORY_CAR_MODE android. intent. category. CAR_MODE 设置该Activity可以在车载环境下使用 CATEGORY_CAR_DOCK android. intent. category. ANSWER
Data数据 l Data属性通常用与Action属性结合使用 l 为Intent提供可操作的数据 l Data属性接收URI对象 n 【语法】 scheme: //host: port/path n 【示例】URI字符串 http: //www. baidu. com 讲师演示讲解 【代码5 - 3】activity_uri. xml 【代码5 - 4】Uri. Activity. java
Extras扩展信息 l Extras属性是一个Bundle对象 l 用于在多个Activity之间交换数据 l Extras属性的使用过程 n 使用Extras属性 Bundle bundle= new Bundle(); bundle. put. String("test", "this is a test"); Intent intent = new Intent(Main. Activity. this, Second. Activity. class); intent. put. Extras(bundle); start. Activity(intent); n 通过get. Extras()方法获得Bundle对象并进行取值 Bundle bundle = this. get. Intent(). get. Extras(); String test = bundle. get. String("test");
Flags标志位 l Flag属性用于为Intent添加额外的控制标志 l 通过Intent的add. Flags()方法为Intent添加控制标志 l 常用的Flag值: n FLAG_ACTIVITY_CLEAR_TOP n FLAG_ACTIVITY_NEW_TASK n FLAG_ACTIVITY_NO_HISTORY n FLAG_ACTIVITY_SINGLE_TOP
5. 1. 3 使用Intent启动Activity l 通过调用Context的start. Activity()方法可以创建并显示目标Activity start. Activity(my. Intent); l start. Activity()方法会查找并启动一个与Intent参数相匹配的Activity l start. Activity. For. Result()方法启动Activity并跟踪子Activity的反馈
显式Intent启动Activity l 通过Intent来显式地指定要打开的Activity l 显式Intent启动Activity Intent intent = new Intent(My. Activity. this, My. Other. Activity. class); start. Activity(intent); 讲师演示讲解 【代码5 - 5】activity_1. xml 【代码5 - 6】activity_2. xml 【代码5 - 7】Activity_1. java 【代码5 - 8】Activity_2. java
l 常见的使用Intent来启动内置应用程序有四种: n 启动浏览器 Intent i=new Intent(Intent. ACTION_VIEW, Uri. parse("http: //www. sohu. com")); start. Activity(i); n 启动地图 Intent i=new Intent(Intent. ACTION_VIEW, Uri. parse("geo: 25. 04692437135412, 121. 5161783959678")); start. Activity(i); n 打电话 Intent i=new Intent(Intent. ACTION_DIAL,Uri. parse("tel: +1234567")); start. Activity(i); n 发送电子邮件 Intent i=new Intent(Intent. ACTION_SENDTO,Uri. parse("mailto: qst@163. com")); start. Activity(i);
l 使用Intent的resolve. Activity()方法进行确认 //创建隐式Intent来启动新的Activity Intent intent =new Intent(Intent. ACTION_DIAL, Uri. parse("tel: 555 -2368")); //检查这个Activity是否存在 Package. Manager pm = get. Package. Manager(); Component. Name cn = intent. resolve. Activity(pm); if (cn == null) { //如果这个Activity不存在则指向the Google Play Store Uri market. Uri =Uri. parse("market: //search? q=pname: com. myapp. packagename"); Intent market. Intent = new Intent(Intent. ACTION_VIEW). set. Data(market. Uri); //如果在Google Play Store中有则下载,否则报错。 if (market. Intent. resolve. Activity(pm) != null){ start. Activity(market. Intent); }else{ Log. d(TAG, "Market client not available. "); } }else{ start. Activity(intent); }
传递数据给其他Activity l 通过Intent的put. Extra()或put. Extras()方法可以向目标Activity传 递数据隐 l 使用put. Extras()方法批量传递数据 Intent intent = new Intent(); Bundle bundle = new Bundle(); //该类用作携带数据 bundle. put. String("name", "中华文明"); bundle. put. String("address", "青岛"); intent. put. Extras(bundle); l 使用put. Extra()方法单个传递数据 Intent intent = new Intent(); intent. put. Extra("name", "中华文明"); 讲师演示讲解 【代码5 - 9】Activity_1. java 【代码5 - 10】Activity_2. java
从Activity返回数据 l 使用start. Activity. For. Result()方法启动一个Activity,新启动的 Activity在关闭时可以原Activity返回数据 l 启动一个目标Activity n 显示启动Activity Intent intent = new Intent(this, My. Other. Activity. class); start. Activity. For. Result(intent, 1); n 隐式启动Activity Uri uri = Uri. parse("content: //contacts/people"); Intent intent = new Intent(Intent. ACTION_PICK, uri); start. Activity. For. Result(intent, 2);
l 从目标Activity中返回数据 n 在目标Activity中调用finish()方法之前,通过set. Result()方法向原 Activity返回一个结果 n set. Result()方法是一个重载方法,其形式如下: ü set. Result(int result. Code)——设置传递到上一个界面的数据 set. Result(Activity. RESULT_CANCELED); ü set. Result(int result. Code, Intent data) ——设置传递到上一个界面的数据 Intent result = new Intent(Intent. ACTION_PICK, selected. Horse); set. Result(Activity. RESULT_OK, result);
处理从目标Activity返回的数据 l 重写on. Activity. Result()方法处理从目标Activity返回的结果 on. Activity. Result(int request. Code, int result. Code, Intent data)); n request. Code是在启动目标Activity时所使用的请求码 n result. Code表示从目标Activity返回的状态码 n data是状态码对应的返回数据 讲师演示讲解 【代码5 - 11】On. Activity. Result. Activity. java 【代码5 - 12】Moon. Activity. java 【代码5 - 13】Mars. Activity. java
5. 1. 4 Intent Filter过滤器 l Intent Filter过滤器用于描述指定的组件可以处理哪些意图 l 在Intent Filter中可以包含Intent对象的三个属性: n ACTION n DATA n CATEGORY l 在Android. Manifest. xml中配置<intent-filter>元素 <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>
l 当Intent对象或者过滤器没有指定<action>时: n 如果一个Intent过滤器没有指定任何<action>,则不会匹配任何Intent; n 如果一个Intent对象没有指定任何<action>,而相应的过滤器中有至少一个 <action>时将自动通过此测试。 l Category测试 <intent-filter> <category android: name="android. intent. category. DEFAULT" /> <category android: name="android. intent. category. BROWSABLE" />. . . </intent-filter>
l Data测试 <intent-filter> <data android: mime. Type="video/mpeg" android: scheme="http: //". . . /> <data android: mime. Type="audio/mpeg" android: scheme="http: //". . . />. . . </intent-filter> l 每个<data >元素可以指定URI和data type属性 l URI属性由schema、host、port和path组成 n 【语法】 schema: //host: port/path n 【示例】URI content: //com. example. project: 200/folder/subfolder/etc
使用Broadcase. Receiver l Broadcast. Receiver的使用步骤: ① 定义一个Broadcase. Receiver的子类 ② 在Android. Manifest. xml文件中注册广播接收器对象 ③ 在Android. Manifest. xml中添加接收和发送短信权限 讲师演示讲解 【代码5 - 14】SMSBroadcast. Receiver. java 【代码5 - 15】在Android. Manifest. xml中注册SMSBroadcast. Receiver类 【代码5 - 16】在Android. Manifest. xml中添加短信的收发权限
5. 3 Handler消息传递机制 l 新启动的线程中更新UI组件 方法 功能描述 handle. Message(Message msg) 通过重写该方法来处理消息 has. Message(int what) 检查消息队列中是否包含what所指定的消息 has. Message(int what,Object object) 检查队列中是否有指定的what和指定对象的消息 obtain. Message() 用于获取消息,具有多个重载方法 send. Empty. Message(int what) 用于发送空消息 send. Empty. Message. Delayed(int what, long delay. Millis) 用于在指定的时间之后发送空消息 send. Message(Message msg) 立即发送消息 send. Message. Delayed(Message msg,long delay. Millis) 用于在指定的时间之后发送空消息
讲师演示讲解 【代码5 - 17】main. xml 【代码5 - 18】Handler. Activity. java 【代码5 - 19】在Android. Manifest. xml中添加访问网络的权限
5. 4 Async. Task类 l Async. Task的语法格式: public abstract class Async. Task<Params, Progress, Result> n Params是启动任务执行的输入参数 n Progress是后台任务执行的进度 n Result是后台计算结果的类型 l 在执行异步任务时,通常会涉及以下几个步骤: ① execute(Params. . . params) ② on. Pre. Execute() ③ do. In. Background ④ on. Progress. Update(Progress. . . values) ⑤ on. Post. Execute(Result result)
5. 5 贯穿任务实现 l 【任务 5 -1】 完成用户日程界面 n user_calendar. xml n user_calendar_item. xml n User. Calendar. Activity. java l 【任务 5 -2】 完成用户日程编辑界面 n user_calendar_event. xml n User. Calendar. Event. Activity. java l 【任务 5 -3】 完成用户日程提醒功能 n user_calendar_remind. xml n User. Calendar. Remind. Activity. java n User. Calendar. Event. Activity. java
- Slides: 44