Android实例剖析笔记notePad

开卷语

      俗话说,“熟读唐诗三百首,不会作诗也会吟”。最近收集了很多Android的示例代码,从这些代码的阅读和实验中学习到很多知识,从而产生写这个系列的打算,目标就是一步步跟着实例进行动手实作,真正从“做”中体会和学习Android开发。
      本文是这个系列的第一篇,目标是Android自带的一个范例程序:记事本,将分为四篇文章进行详细介绍。

预备知识

      搭建开发环境,尝试编写”Hello World”,了解Android的基本概念,熟悉Android的API(官方文档中都有,不赘述)。

程序截图

      先来简单了解下程序运行的效果








程序入口点

   类似于win32程序里的WinMain函数,Android自然也有它的程序入口点。它通过在AndroidManifest.xml文件中配置来指明,可以看到名为NotesList的activity节点下有这样一个intent-filter,其action为android.intent.action.MAIN,

Category指定为 android.intent.category.LAUNCHER,这就指明了这个activity是作为入口activity,系统查找到它后,就会创建这个activity实例来运行,若未发现就不启动(你可以把MAIN改名字试试)。

   < intent-filter >
                 < action  android:name ="android.intent.action.MAIN"   />
                 < category  android:name ="android.intent.category.LAUNCHER"   />
             </ intent-filter >

NotesList详解

      就从入口点所在的activity(见图1)开始,可以看到这个activity最重要的功能就是显示日志列表。这个程序的日志都存放在Sqlite数据库中,因此需要读取出所有的日志记录并显示。

      先来看两个重要的私有数据,第一个PROJECTION字段指明了“日志列表“所关注的数据库中的字段(即只需要ID和Title就可以了)。

    private   static   final  String[] PROJECTION  =   new  String[] {
            Notes._ID,  //  0
            Notes.TITLE,  //  1
    };

     第二个字段COLUMN_INDEX_TITLE指明title字段在数据表中的索引。

private   static   final   int  COLUMN_INDEX_TITLE  =   1 ;

然后就进入第一个调用的函数onCreate

        Intent intent  =  getIntent();
         if  (intent.getData()  ==   null ) 
        {
            intent.setData(Notes.CONTENT_URI);
        }

      因为NotesList这个activity是系统调用的,此时的intent是不带数据和操作类型的,系统只是在其中指明了目标组件是Notelist,所以这里把”content:// com.google.provider.NotePad/notes”保存到intent里面,这个URI地址指明了数据库中的数据表名(参见以后的NotePadProvider类),也就是保存日志的数据表notes

        Cursor cursor  =  managedQuery(getIntent().getData(), PROJECTION,  null ,  null , Notes.DEFAULT_SORT_ORDER);

      然后调用managedQuery函数查询出所有的日志信息,这里第一个参数就是上面设置的” content:// com.google.provider.NotePad/notes”这个URI,即notes数据表。PROJECTION字段指明了结果中所需要的字段,Notes.DEFAULT_SORT_ORDER 指明了结果的排序规则。实际上managedQuery并没有直接去查询数据库,而是通过Content Provider来完成实际的数据库操作,这样就实现了逻辑层和数据库层的分离。

 SimpleCursorAdapter adapter  =   new  SimpleCursorAdapter( this , R.layout.noteslist_item, cursor,
                 new  String[] { Notes.TITLE },  new   int [] { android.R.id.text1 });
        setListAdapter(adapter);

      查询出日志列表后,构造一个CursorAdapter,并将其作为List View的数据源,从而在界面上显示出日志列表。可以看到,第二个参数是R.layout.noteslist_item,打开对应的noteslist_item.xml文件,

< TextView  xmlns:android ="http://schemas.android.com/apk/res/android"
    android:id ="@android:id/text1"
    android:layout_width ="fill_parent"
    android:layout_height ="?android:attr/listPreferredItemHeight"
    android:textAppearance ="?android:attr/textAppearanceLarge"
    android:gravity ="center_vertical"
    android:paddingLeft ="5dip"
    android:singleLine ="true"
/>

      就是用来显示一条日志记录的TextView,最后两个字段指明了实际的字段映射关系,通过这个TextView来显示一条日志记录的title字段。

处理“选择日志”事件

      既然有了“日志列表”,就自然要考虑如何处理某一条日志的单击事件,这通过重载onListItemClick方法来完成,

    @Override
     protected   void  onListItemClick(ListView l, View v,  int  position,  long  id) {
        Uri uri  =  ContentUris.withAppendedId(getIntent().getData(), id);
        
        String action  =  getIntent().getAction();
         if  (Intent.ACTION_PICK.equals(action)  ||  Intent.ACTION_GET_CONTENT.equals(action)) {
             //  The caller is waiting for us to return a note selected by
             //  the user.  The have clicked on one, so return it now.
            setResult(RESULT_OK,  new  Intent().setData(uri));
        }  else  {
             //  Launch activity to view/edit the currently selected item
            startActivity( new  Intent(Intent.ACTION_EDIT, uri));
        }
    }

      首先通过”content:// com.google.provider.NotePad/notes”和日志的id 号拼接得到选中日志的真正URI,然后创建一个新的Intent,其操作类型为Intent.ACTION_EDIT,数据域指出待编辑的日志URI(这里只分析else块)。

Intent深度剖析

那么,上面这句startActivity(new Intent(Intent.ACTION_EDIT, uri))执行后会发生什么事情呢?这时候Android系统就跳出来接管了,它会根据intent中的信息找到对应的activity,在这里找到的是NoteEditor这个activity,然后创建这个activity的实例并运行。

那么,Android又是如何找到NoteEditor这个对应的activity的呢?这就是intent发挥作用的时刻了。

new  Intent(Intent.ACTION_EDIT, uri)

这里的Intent.ACTION_EDIT=” android.intent.action.EDIT”,另外通过设置断点,我们看下这里的uri值:

      可以看到选中的日志条目的URI是:content://com.google.provider.NotePad/notes/1

然后我们再来看下Androidmanfest.xml,其中有这个provider

< provider  android:name ="NotePadProvider"
            android:authorities ="com.google.provider.NotePad"
         />

      发现没有?它也有com.google.provider.NotePad,这个是content://com.google.provider.NotePad/notes/1的一部分,同时

     < activity  android:name ="NoteEditor"
            android:theme ="@android:style/Theme.Light"
            android:label ="@string/title_note"
            android:screenOrientation ="sensor"
            android:configChanges ="keyboardHidden|orientation"
         >
             <!--  This filter says that we can view or edit the data of
                 a single note  -->
             < intent-filter  android:label ="@string/resolve_edit" >
                 < action  android:name ="android.intent.action.VIEW"   />
                 < action  android:name ="android.intent.action.EDIT"   />
                 < action  android:name ="com.android.notepad.action.EDIT_NOTE"   />
                 < category  android:name ="android.intent.category.DEFAULT"   />
                 < data  android:mimeType ="vnd.android.cursor.item/vnd.google.note"   />
             </ intent-filter >
             <!--  This filter says that we can create a new note inside
                 of a directory of notes.  -->
             < intent-filter >
                 < action  android:name ="android.intent.action.INSERT"   />
                 < category  android:name ="android.intent.category.DEFAULT"   />
                 < data  android:mimeType ="vnd.android.cursor.dir/vnd.google.note"   />
             </ intent-filter >
         </ activity >

      上面第一个intent-filter中有一个action 名为android.intent.action.EDIT,而前面我们创建的Intent也正好是

Intent.ACTION_EDIT=” android.intent.action.EDIT”,想必大家已经明白是怎么回事了吧。

下面就进入activity选择机制了:

系统从intent中获取道uri,得到了content://com.google.provider.NotePad/notes/1,去掉开始的content:标识,得到com.google.provider.NotePad/notes/1,然后获取前面的com.google.provider.NotePad,然后就到Androidmanfest.xml中找到authorities为com.google.provider.NotePad的provider,这个就是后面要讲的contentprovider,然后就加载这个content provider。

         < provider  android:name ="NotePadProvider"
            android:authorities ="com.google.provider.NotePad"
         />

在这里是NotePadProvider,然后调用NotePadProvidergettype函数,并把上述URI传给这个函数,函数返回URI所对应的类型(这里返回Notes.CONTENT_ITEM_TYPE,代表一条日志记录,而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note ")。

   @Override
     public  String getType(Uri uri) {
         switch  (sUriMatcher.match(uri)) {
         case  NOTES:
             return  Notes.CONTENT_TYPE;
         case  NOTE_ID:
             return  Notes.CONTENT_ITEM_TYPE;
         default :
             throw   new  IllegalArgumentException( " Unknown URI  "   +  uri);
        }
}

      上面的sUriMatcher.match是用来检测uri是否能够被处理,而sUriMatcher.match(uri)返回值其实是由

        sUriMatcher  =   new  UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(NotePad.AUTHORITY,  " notes " , NOTES);
        sUriMatcher.addURI(NotePad.AUTHORITY,  " notes/# " , NOTE_ID);

决定的。

然后系统使用获得的" vnd.android.cursor.item/vnd.google.note "和”android.intent.action.EDIT”到androidmanfest.xml中去找匹配的activity.

   < intent-filter  android:label ="@string/resolve_edit" >
                 < action  android:name ="android.intent.action.VIEW"   />
                 < action  android:name ="android.intent.action.EDIT"   />
                 < action  android:name ="com.android.notepad.action.EDIT_NOTE"   />
                 < category  android:name ="android.intent.category.DEFAULT"   />
                 < data  android:mimeType ="vnd.android.cursor.item/vnd.google.note"   />
             </ intent-filter >

正好NoteEditor这个activityintent-filter满足上述条件,这样就找到了NoteEditor。于是系统加载这个类并实例化,运行,然后就到了NoteEditorOnCreate函数中(见后续文章)。

 

小技巧

1,在命令行中使用”adb shell”命令进入系统中,然后”cd app”进入应用程序所在目录,”rm XXX”就可以删除你指定的apk,从而去掉其在系统顶层界面占据的图标,若两次”cd data”则可以进入应用程序使用的数据目录,你的数据可以保存在这里,例如Notepad就是把其数据库放在它的databases目录下,名为note_pad.db.

2,第一次启动模拟器会比较慢,但以后就别关闭模拟器了,修改代码,调试都不需要再次启动的,直接修改后run或debug就是。

作者:phinecos(洞庭散人)
出处:http://phinecos.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但请保留此段声明,并在文章页面明显位置给出原文连接

作者:洞庭散人

出处:http://phinecos.cnblogs.com/    

本博客遵从 Creative Commons Attribution 3.0 License ,若用于非商业目的,您可以自由转载,但请保留原作者信息和文章链接URL。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android核心技术与实例详解 图书简介: 本书以Android应用程序的开发为主题 并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程 全书分为三篇共18章 第一篇以简单易懂的实例为依托 详细介绍了Android平台的基本控件 高级控件 常用界面布局及菜单对话框等相关知识;第二篇介绍了Android平台的高级知识 包括消息与广播服务 网络数据处理 手机通信功能 桌面组件 多媒体采集 Google服务及3D程序开发等;第三篇则对三个真实案例的开发步骤进行详细介绍 逐步向读者讲解Android手机应用程序的真实开发过程 同时在源代码中还包含了详细的注释 以尽量帮助读者掌握代码中的每一个细节 尽快掌握Android编程 本书的讲述由浅入深 从介绍Android平台的基本组件到带领读者开发大型应用程序 结构清晰 语言简洁 非常适合初学者和进阶开发者阅读参考 本书附赠DVD光盘1张 其中包含了大容量的手把手教学视频 电子教案 PPT 实例源代码等 Android核心技术与实例详解 图书目录: 基础篇 第1章 Android开发起步 1 1 Android平台简介 1 1 1 Android背景介绍 1 1 2 Android平台架构 1 2 Android开发环境的搭建 1 2 1 相关软件的下载与安装 1 2 2 虚拟设备的创建和使用 1 2 3 创建“Hello Android” 1 2 4 Android应用程序的调试 1 3 小结 第2章 Android应用程序的构成 2 1 Android应用程序的解析 2 1 1 目录结构 2 1 2 资源的管理与使用 2 1 3 AndroidManifest xml简介 2 1 4 应用程序的权限 2 2 Android基本组件的介绍 2 2 1 应用程序的生命周期 2 2 2 Activity简介 2 2 3 Service简介 2 2 4 BroadcastReceiver简介 2 2 5 ContentProvider简介 2 2 6 Intent和IntentFilter简介 2 3 小结 第3章 Android布局管理器 3 1 控件类概述 3 1 1 View类简介 3 1 2 ViewGroup类简介 3 2 线性布局 3 2 1 LinearLayout类简介 3 2 2 线性布局案例 3 3 表格布局 3 3 1 TableLayout类简介 3 3 2 表格布局案例 3 4 相对布局 3 4 1 RelativeLayout类简介 3 4 2 相对布局案例 3 5 帧布局 3 5 1 FrameLayout类简介 3 5 2 帧布局案例 3 6 绝对布局 3 6 1 AbsoluteLayout类简介 3 6 2 绝对布局案例 3 7 小结 第4章 Android常用基本控件 4 1 文本控件的介绍 4 1 1 TextView类简介 4 1 2 EditText类简介 4 1 3 文本框使用案例 4 2 按钮控件 4 2 1 Button类简介 4 2 2 ImageButton类简介 4 2 3 9Patch图片简介 4 2 3 9Patch图片使用案例 4 3 状态开关按钮 4 3 1 ToggleButton类简介 4 3 2 开关按钮的使用 4 4 单选按钮与复选按钮 4 4 1 CheckBox和RadioButton类简介 4 4 2 单选按钮和复选按钮使用案例 4 5 图片控件 4 5 1 ImageView类简介 4 5 2 图片查看器 4 6 时钟控件 4 6 1 AnalogClock和DigitalClock类简介 4 6 2 时钟控件使用案例 4 7 日期与时间选择控件 4 7 1 DatePicker类简介 4 7 2 TimePicker类简介 4 7 3 日期时间控件使用案例 4 8 动画播放技术 4 8 1 帧动画简介 4 8 2 帧动画使用案例 4 8 3 补间动画简介 4 8 4 补间动画使用案例 4 9 小结 第5章 Android常用高级控件 第6章 菜单与对话框 高 级 篇 第7章 Android事件处理模型 第8章 游戏与3D应用程序开发 第9章 消息广播与服务 第10章 网络与数据处理 第11章 手机通信功能开发 第12章 手机特有Feature开发 第13章 桌面组件与多媒体数据采集 第14章 传感器应用的开发 第15章 Google服务 第16章 Android游戏开发实践 快乐数独 案例篇 第17章 Android地图搜索应用 美食天下 第18章 Android社交分享平台 口袋微博">Android核心技术与实例详解 图书简介: 本书以Android应用程序的开发为主题 并结合真实的案例向读者详细介绍了Android的基本组件的使用及应用程序开发的整个流程 全书分为三篇共18章 第一篇以简单易懂的实例为依托 详细 [更多]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值