这是我参与「第四届青训营 」笔记创作活动的第四天
界面组件
| 界面组件 | 说明 | |
|---|---|---|
| 1.1 | Activity | 界面容器 |
| 1.2 | fragment | 轻量级界面容器 |
| 1.3 | service | 后台服务 |
| 1.4 | broadcast | 广播组件 |
| 1.5 | ContentProvider | 数据组件 |
| 1.6 | Intent | 意图组件 |
1.3 service
service基本用法
-
注册:AndroidManifest中使用<service...l>标签
-
创建:建立相应的Service实现类
-
加载:startService() / bindService()
service生命周期
service 与 Activity 通信
-
定义Binder子类,并实现getService()方法,返回Service对象
-
实现Service类onBind()方法,返回上述Binder对象
-
实例化ServiceConnection对象,实现onServiceConnected()方法,从中获取到Service实例
-
Activity中调用bindService()方法,并传递步骤3的ServiceConnection对象,将流程跑起来
-
Activity既可以通过调用Service实例中的方法进行直接通信
1.4 broadcast
broadcast 基本用法
静态广播
1 注册:AndroidManifest中使用<receiver.../><intent-filter.../>
-
创建:建立相应的BroadcastReceiver实现类
-
接收:在步骤2类onReceive()中接收广播
-
发送:Context.sendBroadcast()
动态广播
注册:Context.registerReceiver()
常用系统广播
-
lntent.ACTION_CONNECTIVITY_CHANGE
-
lntent.ACTION_BATTERY_CHANGED
-
Intent.ACTION_SCREEN_ON
-
Intent.ACTION_SCREEN_OFF
-
Intent.ACTION_PACKAGE_INSTALL
-
lntent.ACTION_BOOT_COMPLETED
-
lntent.ACTION_PACKAGE_ADDED
-
Intent.ACTON_PACKAGE_REPLACED
-
Intent.ACTION_PACKAGE_REMOVED
1.5 ContentProvider
ContentProvider 基本用法
生产者
-
注册:AndroidManifest中使用<provider.../>
属性: authorities / exported / readPermission / writePermission
-
创建:建立相应的ContentProvider实现类
方法:onCreate / getType / insert / delete / update / query
消费者
-
声明:AndroidManifest中声明权限
-
使用:context.getContentResolver()方法:
insert / delete / update / query
cur = MediaStore. Images.Thumbnails.queryMiniThumbnails( context.getContentResolver(), MediaStore. Images. Thumbnails.EXTERNAL_CONTENT_URI, MediaStore. Images. Thumbnails.MINI_KIND,projection); if (cur != null && cur.moveToFirst()) { do { String imageld = cur.getString( cur.getColumnIndex(MediaStore.Images. Thumbnails.IMAGE_ID)); String imagePath = cur.getString( cur.getColumnIndex(MediaStore.Images, Thumbnails.DATA)); thumbnailMap. put(imageId,imagePath); } while (cur.moveToNext( && ! cur.isLast()); }
1.6 Intent
Intent基本用法
-
显式lntent
setComponent / setClass指定具体类
-
隐式Intent
- Action(动作)
- Data(数据)
- Category(类别)
- Type(数据类型)
- Component(组件)
- Extra(扩展信息)
- Flag(标志位)
系统能力
使用: startActivity(intent)
- 电话:Intent(Intent.ACTION_DIAL, Uri.parse("tel:10010"))
- 短信:Intent(Intent.ACTION_SENDTo, Uri.parse("smsto:10010"))
- 网页:Intent(Intent.ACTION_VIEW,Uri.parse("www.baidu.com"))
- 邮件:Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:someone@domain.com"))
- 地图:Intent(Intent.ACTION_VIEW, Uri.parse("geo:39.9,116.3"))
- 拍照: Intent(MediaStore.ACTION_IMAGE_CAPTURE)
- 设置:Intent(android.provider.Settings.ACTION_SETTINGS)
- 市场:Intent(Intent.ACTION_VIEW, Uri.parse("market:lldetails?id=" + packageName))