Android(安卓)Sample NotePad学习一
今天起学习一下Android自带的记事本NotePad的例子,里面的代码有很多值得学习的
在启动Activity中NotesList的onCreate()感觉写的非常不错,学习一下:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setDefaultKeyMode(DEFAULT_KEYS_SHORTCUT); // If no data was given in the intent (because we were started // as a MAIN activity), then use our default content provider. Intent intent = getIntent(); if (intent.getData() == null) { intent.setData(Notes.CONTENT_URI); } // Inform the list we provide context menus for items getListView().setOnCreateContextMenuListener(this); // Perform a managed query. The Activity will handle closing and requerying the cursor // when needed. Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, Notes.DEFAULT_SORT_ORDER); // Used to map notes entries from the database to views SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.noteslist_item, cursor, new String[] { Notes.TITLE }, new int[] { android.R.id.text1 }); setListAdapter(adapter); }
Action
一个字符串, 为请求的动作命名, 或者, 对于broadcast intent, 发生的并且正在被报告的动作. 例如:
常量 | 目标组件 | 动作 |
---|---|---|
ACTION_CALL | activity | 发起一个电话呼叫. |
ACTION_EDIT | activity | 显示数据给用户来编辑. |
ACTION_MAIN | activity | 将该activity作为一个task的第一个activity启动,不传入参数也不期望返回值. |
ACTION_SYNC | activity | 将设备上的数据和一个服务器同步. |
ACTION_BATTERY_LOW | broadcast receiver | 发出电量不足的警告. |
ACTION_HEADSET_PLUG | broadcast receiver | 一个耳机正被插入或者拔出. |
ACTION_SCREEN_ON | broadcast receiver | 屏幕被点亮. |
ACTION_TIMEZONE_CHANGED | broadcast receiver | 时区设置改变. |
你也可以定义自己的action字符串用来启动你的应用程序. 自定义的action应该包含应用程序的包名.例如"com.example.project.SHOW_COLOR".
action很大程度上决定了intent的另外部分的结构, 就像一个方法名决定了它接受的参数和返回值一样. 因此, 最好给action一个最能反映其作用的名字.
一个intent对象中的action是使用getAction()和setAction()来读写的.
Data
需要操作的数据的URI和它的 MIME(多用途互联网邮件扩展,Multipurpose Internet Mail Extensions)类型. 例如, 如果action为ACTION_EDIT, 那么Data将包含待编辑的数据URI. 如果action为ACTION_CALL, Data将为tel:电话号码的URI. 如果action为ACTION_VIEW, 则Data为http:网络地址的URI.
当将一个intent和一个组件相匹配时, 除了URI外数据类型也很重要. 例如, 一个显示图片的程序不应该用来处理声音文件.
数据类型常常可以从URI推断, 特别是content:URI, 它表示该数据属于一个content provider. 但数据类型也可以被intent对象显示声明. setData()方法设置URI, 而setType()方法指定MIME类型, setDataAndType()设置数据URI和MIME类型. 它们可以使用getData()和getType()来读取.
// If no data was given in the intent (because we were started // as a MAIN activity), then use our default content provider. Intent intent = getIntent(); if (intent.getData() == null) { intent.setData(Notes.CONTENT_URI); }
之前有一段时间认为intent.getData()返回的是<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />这里面指定的类型,而其实这是intent.getType()返回的类型,intent.getData()返回的是URI
更多相关文章
- 全球支持最多运行平台的NoSQL数据库 iBoxDB
- 创建适配器时报:ERROR/AndroidRuntime(1703): Caused by: androi
- Android(安卓)安全 —— 本地拒绝服务漏洞
- Android(安卓)Sqlite Failed to open database(无法打开数据库文
- 做android 开发遇到的问题及解决办法记录
- 基于Java LinkedList,实现Android大数据缓存策略
- SharedPreferences之Android数据保存
- 【Android笔记】探究活动②使用Intent在活动之间穿梭
- mybatisplus的坑 insert标签insert into select无参数问题的解决