[Android 小应用]batterymonitor 电源监控

来源:互联网 发布:p2p返利的理财源码 编辑:程序博客网 时间:2024/05/17 09:26

需求分析

  1. ForeGroundService.
    阅读材料1:
    材料链接
    具体实现
    foreground service是用来指示一些客户明了的Service,当系统内存不足时不会被回收。但是foreground service必须在状态栏提供notification,这个notification必须一直保持。
    例如音乐播放器可以使用foreground service,然后在notification中显示当前的歌曲等信息,并且可以在notification中和音乐播放器进行交互,例如选择下一首歌等。代码如下:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),        System.currentTimeMillis());Intent notificationIntent = new Intent(this, ExampleActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(this, getText(R.string.notification_title),        getText(R.string.notification_message), pendingIntent);startForeground(ONGOING_NOTIFICATION, notification);

设计分析

  1. 先把最基本的Service 温习了一遍
    材料地址1
    材料地址2

startService和bindService的主要区别应该是bindService绑定后与service通信是在调用的Activity里面重写OnServiceConnected,service与用户不能进行直接的交互。

  1. 在上面的Service使用中,参考使用下面的Notification。
    详细介绍了Notifcation中各个参数的使用。

  2. 在这个中间涉及到Notification的尺寸的图片尺寸。SmallIconSize
    You should check to see that your small icon follows the UX guidelines for icon size. Small icons are limited to 24x24dp.
    大图片尺寸可以通过命令获取,得到宽高均为64dp。

int width = (int) getResources().getDimension(android.R.dimen.notification_large_icon_width);        int height = (int) getResources().getDimension(android.R.dimen.notification_large_icon_height);
  1. 参照上面具体实现的链接,实现了前台Service的实现。
    具体实现

  2. 需要对BroadcastReceiver进行实现
    参考链接

  3. 改变模拟器的电量显示。
    参考链接

6.1 查看模拟器的端口如5554。

telnet localhost 5554

6.2 使用命令调节电量。

power capacity 81

其他的power的子命令如下
display display battery and charger state

ac set AC charging state

status           set battery statuspresent         set battery present statehealth           set battery health statecapacity         set battery capacity state
  1. 在发送Notifcation 过程中,因为使用了定时更新进度的方法,需要考虑怎样update。后来发现其实主要是进行builder的重写,重写完成后,manager去notify就可以了。这里需要注意的是我们已经使用那个startForeground调用notification去保证这个service不被轻易kill,所以使用过一次notification了,这里只要把builder重新设置值,然后用notifcationManger去update就可以。
    重复notify

  2. 为了开机自启动,可以使用如下链接中提供的方法,在启动的时候用广播接收器启动service.
    开机自启动

阅读源材料1

材料地址

  1. 其中第三点提到了 vold server
三、 vold server分析 1、在 system/vold/NetlinkManager.cpp中: 2、然后在 system/vold/NetlinkHandler.cpp的 NetlinkHandler::onEvent中处理 3、在 system/core/libsysutils/src/NetlinkListener.cpp中监听

针对Vold Server找到如下链接,其中就提到这个volume daemon是守护进程。链接中给出的文章详细介绍了热插拔的流程。材料

  1. 然后第四点提到了battery server的分析。

2.1 BatteryService.java就是我们主要要完成的service,该service一开始就被加入到了system_process中,而我们的需求是自己编写一个batteryservice练习。
2.2 其中这些数据是从底层JNI(com_android_server_BatteryService.cpp)来读取数据。
2.3 BatteryService把电池的信息通过intent广播发送出去。
2.4 需要处理这个电池信息,只需要注册广播接收器,接受这个电池变化的信息。
2.5系统会不断的UEvent来发送信息。

0 0
原创粉丝点击