Service 动态更新 UI
http://blog.csdn.net/u013724061/article/details/38642049
最终效果:
动态显示当前时间和电量
思路:
首先在Activity里用内部类定义两种广播,一种是系统广播(电池),另一种是自定义广播。
private class SimpleBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
int currLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, ); //当前电量
int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE, ); //总电量
int percent = currLevel * / total;
updateG(percent);
Log.i(TAG, "battery: " + percent + "%");
}
} private class UpdateTimeReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); String dateStr = dateFormat.format(date);
String timeStr = timeFormat.format(date); tv_date.setText(dateStr + ":" + timeStr);//显示出日期
}
}
在OnResume中注册广播,OnPause中移除 (文章中有为什么不在onStart和onStop中的解释)
@Override
protected void onResume() {
super.onResume(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
simpleBroadcastReceiver = new SimpleBroadcastReceiver();
registerReceiver(simpleBroadcastReceiver, intentFilter); intentFilter = new IntentFilter("ACTION_UPDATE_TIME");
updateTimeReceiver = new UpdateTimeReceiver();
registerReceiver(updateTimeReceiver, intentFilter); Intent intent = new Intent(this,UpdateGService.class);
startService(intent);
} @Override
protected void onPause() {
super.onPause();
unregisterReceiver(simpleBroadcastReceiver);
unregisterReceiver(updateTimeReceiver);
Intent intent = new Intent(this,UpdateGService.class);
stopService(intent);
}
建立个service来处理动态更新
public class UpdateGService extends Service {
@Override
public void onCreate() {
super.onCreate();
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Intent timeIntent = new Intent();
timeIntent.setAction("ACTION_UPDATE_TIME");//自定义Action
sendBroadcast(timeIntent); //发送广播
}
}, , ); //每隔1秒
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
最终效果:

Service 动态更新 UI的更多相关文章
- 在Android中实现service动态更新UI界面
之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...
- android中实现service动态更新UI界面
案例:通过service向远程服务器发送请求,根据服务器返回的结果动态更新主程序UI界面,主程序可实时关闭或重启服务. 注册BroadcastReceiver 在主程序activity中注册一个Bro ...
- Android Handler传递参数动态更新UI界面demo
package com.example.demo_test; import android.app.Activity; import android.os.Bundle; import android ...
- 动态更新UI的方式
1. TimerTask 和 timer连用: 这里主要是实现倒计时, TimerTask 里面有方法runOnUiThread,在这个方法里面调用timer cancel()停止倒计时,同样更新UI ...
- Android UI:机智的远程动态更新策略
问题描述 做过Android开发的人都遇到过这样的问题:随着需求的变化,某些入口界面通常会出现 UI的增加.减少.内容变化.以及跳转界面发生变化等问题.每次发生变化都要手动修改代码,而入口界面通常具有 ...
- Mono for android 如何动态添加View,线程内部如何更新UI.
貌似所有设计到UI的程序原理都是一样的,子线程是不能够更新UI状态的,所以就必须使用UI自身或者第三方来更新UI. 如 在WinForm 中 就可以使用Control.Invoke(Action ac ...
- Service和广播联合更新UI的例子
sa111111 于 2010-11-19 10:56 发表在 [Android实例] [复制链接] [只看楼主] [上一主题] [下一主题] 在Android中,异步更新UI,通常我们会选用Ha ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- 【用户交互】APP没有退出前台但改变系统属性如何实时更新UI?监听系统广播,让用户交互更舒心~
前日,一小伙伴问我一个问题,说它解决了半天都没解决这个问题,截图如下: 大概楼主理解如下: 如果在应用中有一个判断wifi的开关和一个当前音量大小的seekbar以及一个获取当前电量多少的按钮,想知道 ...
随机推荐
- 格式化json日期'/Date(-62135596800000)/'
日期经过json序列化之后,变成了'/Date(-62135596800000)/'字符串,在显示数据时,我们需要解释成正常的日期. Insus.NET和js库中,写了一个jQuery扩展方法: $. ...
- Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Linux巩固记录(8) Hbase shell 基本使用
继续前几篇内容,讲解hbase基本使用 1.进入hbase shell: hbase有很多种操作方式,比如shell,java客户端,webUI等,可以直接输入hbase进行提示 [root@mast ...
- 设置 Linux 下打印机的几种方式
设置 Linux 下打印机的几种方式 一.使用 cups 进行设置 如若遇到 cups 也没有驱动的话可以前往 openprinting.org 找寻对应驱动. 二.前往 official 下载驱动 ...
- postgresql-日志表
pg_log,数据库日志表postgresqllog CREATE TABLE postgres_log ( log_time timestamp(3) with time zone, 日志生成时间 ...
- postgresql-hdd,ssd,效率
既有ssd又有hdd是将数据存储到ssd还是将索引存储到ssd的效率更高呢? 一种说法是索引是随机扫描,将索引放入ssd效率会增高, 一种说法是将数据放入ssd效率更高 最好的情况是将数据和索引都 ...
- 解决 在 WINDOWS 下 同时安装 python2 python3 后 pip 错误
再之前同时安装 python 后 只需把环境变量PATH 里面改为 PATH=C:\Python36-32\Scripts\;C:\Python36-32\;C:\Python27\;C:\Pytho ...
- Redis学习系列四Hash(字典)
一.简介 Redis中的Hash字典相当于C#中的Hashtable,是一种无序字典,内存存储了很对的键值对,实现上和Hashtable一样,都是"数组+链表"二维结构,都是对关键 ...
- Android分组子级的不同视图布局之BUG奇遇记
Android分组子级的不同视图布局之BUG奇遇记 最近在使用按日期分类列表,二级条目可能不一样,于是就想到了ExpandableListView. ExpandableListView的布局显示分割 ...
- PyCharm引入python需要使用的包
在学习python的时候,被推荐了使用PyCharm这款IDE,但是在import包的时候却发生了问题- -无法找到相应的包,但是明明通过pip安装成功了 在这款IDE中,要导入包,需要手动进行引入 ...