博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 在onReciver里面使用定时器 定时更新UI的例子
阅读量:6902 次
发布时间:2019-06-27

本文共 2675 字,大约阅读时间需要 8 分钟。

android 在onReciver里面使用定时器 定时更新UI的例子
在定时器线程里面是不能直接更新UI的,需要使用Handle,使用Handle可以注册个广播,在定时到时且条件符合情况下发送一个广播
Handler收到广播后处理更新UI相关操作,这里还演示了下传送Context变量作为定时器的构造函数值的方法(如果需要在run方法使用到Context可以用)

下面代码是工程里面的,需要参考的人把无关的代码删除了 留下骨架改成自己的就可以了 

 1 
private 
final BroadcastReceiver mConn_Receiver = 
new BroadcastReceiver() {
 2         @Override
 3         
public 
void onReceive(Context context, Intent intent) {
 4             String action = intent.getAction();
 5             
if (BluetoothGatt.ACTION_GATT_CONNECTED.equals(action)) {
 6                 
//
启动定时器
 7 
                proximityAlarm(2,context);
 8             }
 9         }
10     }
11 
12 
private 
final 
static String PROXIMITY_ALARM_ACTION = "com.cyberblue.iitag.proximityalarm";
13     
private ProximityAlarmRecevier pReceiver;
14     
//
延迟报警
15 
    
public 
void proximityAlarm(
int second,Context context){
16         Log.i(TAG,"Enter proximityAlarm second=" + second);
17         
18         
if(pReceiver == 
null){
19             pReceiver = 
new ProximityAlarmRecevier();
20             registerReceiver(pReceiver, 
new IntentFilter(PROXIMITY_ALARM_ACTION));
21         }
22         
23         Timer time = 
new Timer(); 
24         ProximityAlarmTimerTask task = 
new ProximityAlarmTimerTask(context); 
25         
//
dalay/1000秒后执行task.只执行一次
26 
        time.schedule(task, second*1000);
27     }
28     
29 
public 
class ProximityAlarmTimerTask 
extends TimerTask {
30         
private Context context;
31         
public ProximityAlarmTimerTask(Context mcontext){
32             context = mcontext;
33         }
34         
public 
void run(){
35         Log.i(TAG,"Enter ProximityAlarmTimerTask");
36             
if(mLeState == CONNECTED){
37                 
return ;
38             }
else{
39                 Intent intent = 
new Intent(PROXIMITY_ALARM_ACTION);
40                 sendBroadcast(intent);
41             }
42         } 
43     }
44     
45     
public 
class ProximityAlarmRecevier 
extends BroadcastReceiver {
46         @Override
47         
public 
void onReceive(Context context, Intent intent) {
48             Log.i(TAG,"Enter ProximityAlarmRecevier");
49             pHandler.sendEmptyMessage(0);
50         }
51     }
52     
53     
//
处理重连
54 
    Handler pHandler = 
new Handler() {
55         
public 
void handleMessage(Message msg) {
56             Log.i(TAG,"Enter pHandler");
57             
switch (msg.what) {
58             
case 0:
59                 
//
停止上一次的音乐
60 
                stopMusic();
61                 
//
停止振动
62 
                cancelVibrator();
63                 
//
点亮屏幕
64 
                
final PowerManager.WakeLock wl = wekelock();
65                 vibrator();
66                 playMusic();
67                 
//
只保留最后一个报警窗口
68 
                
if(view != 
null){
69                     wm.removeView(view);
70                 }
71                 view = inflater.inflate(R.layout.proximity_result, 
null);
72                 wm.addView(view, wmParams);
73                 Button proximitytagBtn = (Button) view.findViewById(R.id.proximitytagBtn);
74                 proximitytagBtn.setOnClickListener(
new OnClickListener() {
75                     @Override
76                     
public 
void onClick(View v) {
77                         
//
移除WindowManager
78 
                        wm.removeView(view);
79                         view = 
null;
80                         
//
停止音乐
81 
                        stopMusic();
82                         
//
停止振动
83 
                        cancelVibrator();
84                         
if(wl != 
null && wl.isHeld()){
85                             wl.release();
86                         }
87                     }
88                 });
89                 closeWakeLock(120,wl);
90                 
91                 
break;
92             }
93             
super.handleMessage(msg);
94         }
95     };

转载地址:http://dapdl.baihongyu.com/

你可能感兴趣的文章
展讯NAND Flash高级教程【转】
查看>>
DynamicPropertyAccessor Expression lambda
查看>>
csc.rsp Invent by Microshaoft
查看>>
CentOS7上GitHub/GitLab多帐号管理SSH Key
查看>>
字符串+变量
查看>>
style
查看>>
Log4j配置详解
查看>>
myql 查询树形表结果:说说、说说的述评、评论的回复
查看>>
java基础讲解02-----eclipse快捷方式(2017-04-12 23:47)
查看>>
Eclipse使用Tomcat发布项目时出现YadisException异常解决方案
查看>>
How to do Mathematics
查看>>
[洛谷P4245]【模板】任意模数NTT
查看>>
iOS开发-面试总结(十六)
查看>>
[LeetCode] Unique Binary Search Trees II dfs 深度搜索
查看>>
升级Mac OS X上的git
查看>>
python基础2(数据类型、数据运算、for循环、while循环、列表)
查看>>
应用被强杀了怎么办
查看>>
jquery validate 插件使用心得
查看>>
Windows下安装mysql后,不知道root密码,如果修改root密码
查看>>
Linuxドライバ_LDD3メモ_ハードウェアとの通信
查看>>