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 };
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 };