android 在onReciver里面使用定时器 定时更新UI的例子
2012-08-08 09:30
435 查看
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 }
}
private final static String PROXIMITY_ALARM_ACTION = "com.cyberblue.iitag.proximityalarm";
private ProximityAlarmRecevier pReceiver;
//延迟报警
public void proximityAlarm(int second,Context context){
Log.i(TAG,"Enter proximityAlarm second=" + second);
if(pReceiver == null){
pReceiver = new ProximityAlarmRecevier();
registerReceiver(pReceiver, new IntentFilter(PROXIMITY_ALARM_ACTION));
}
Timer time = new Timer();
ProximityAlarmTimerTask task = new ProximityAlarmTimerTask(context);
//dalay/1000秒后执行task.只执行一次
time.schedule(task, second*1000);
}
public class ProximityAlarmTimerTask extends TimerTask {
private Context context;
public ProximityAlarmTimerTask(Context mcontext){
context = mcontext;
}
public void run(){
Log.i(TAG,"Enter ProximityAlarmTimerTask");
if(mLeState == CONNECTED){
return ;
}else{
Intent intent = new Intent(PROXIMITY_ALARM_ACTION);
sendBroadcast(intent);
}
}
}
public class ProximityAlarmRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"Enter ProximityAlarmRecevier");
pHandler.sendEmptyMessage(0);
}
}
//处理重连
Handler pHandler = new Handler() {
public void handleMessage(Message msg) {
Log.i(TAG,"Enter pHandler");
switch (msg.what) {
case 0:
//停止上一次的音乐
stopMusic();
//停止振动
cancelVibrator();
//点亮屏幕
final PowerManager.WakeLock wl = wekelock();
vibrator();
playMusic();
//只保留最后一个报警窗口
if(view != null){
wm.removeView(view);
}
view = inflater.inflate(R.layout.proximity_result, null);
wm.addView(view, wmParams);
Button proximitytagBtn = (Button) view.findViewById(R.id.proximitytagBtn);
proximitytagBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//移除WindowManager
wm.removeView(view);
view = null;
//停止音乐
stopMusic();
//停止振动
cancelVibrator();
if(wl != null && wl.isHeld()){
wl.release();
}
}
});
closeWakeLock(120,wl);
break;
}
super.handleMessage(msg);
}
};
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 }
}
private final static String PROXIMITY_ALARM_ACTION = "com.cyberblue.iitag.proximityalarm";
private ProximityAlarmRecevier pReceiver;
//延迟报警
public void proximityAlarm(int second,Context context){
Log.i(TAG,"Enter proximityAlarm second=" + second);
if(pReceiver == null){
pReceiver = new ProximityAlarmRecevier();
registerReceiver(pReceiver, new IntentFilter(PROXIMITY_ALARM_ACTION));
}
Timer time = new Timer();
ProximityAlarmTimerTask task = new ProximityAlarmTimerTask(context);
//dalay/1000秒后执行task.只执行一次
time.schedule(task, second*1000);
}
public class ProximityAlarmTimerTask extends TimerTask {
private Context context;
public ProximityAlarmTimerTask(Context mcontext){
context = mcontext;
}
public void run(){
Log.i(TAG,"Enter ProximityAlarmTimerTask");
if(mLeState == CONNECTED){
return ;
}else{
Intent intent = new Intent(PROXIMITY_ALARM_ACTION);
sendBroadcast(intent);
}
}
}
public class ProximityAlarmRecevier extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG,"Enter ProximityAlarmRecevier");
pHandler.sendEmptyMessage(0);
}
}
//处理重连
Handler pHandler = new Handler() {
public void handleMessage(Message msg) {
Log.i(TAG,"Enter pHandler");
switch (msg.what) {
case 0:
//停止上一次的音乐
stopMusic();
//停止振动
cancelVibrator();
//点亮屏幕
final PowerManager.WakeLock wl = wekelock();
vibrator();
playMusic();
//只保留最后一个报警窗口
if(view != null){
wm.removeView(view);
}
view = inflater.inflate(R.layout.proximity_result, null);
wm.addView(view, wmParams);
Button proximitytagBtn = (Button) view.findViewById(R.id.proximitytagBtn);
proximitytagBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//移除WindowManager
wm.removeView(view);
view = null;
//停止音乐
stopMusic();
//停止振动
cancelVibrator();
if(wl != null && wl.isHeld()){
wl.release();
}
}
});
closeWakeLock(120,wl);
break;
}
super.handleMessage(msg);
}
};
相关文章推荐
- Android中使用定时器更新UI
- Android开发常见错误(3):使用retrofit+rxjava更新UI出现卡顿
- Android编写简单的手机通讯录,只是存到数据库里面,并没有显示出来,下次更新显示到自定义适配器的通讯录(这个只是教大家SQLite数据库如何使用)
- android 使用asynctask结合fragment更新UI(另附线程池管理示例)
- Android使用handler实现简单的定时器,主线程每秒刷新UI
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- AndroidUI--SlidingMenu使用例子
- 2、 AndroidUI--SlidingMenu使用例子
- Android使用线程更新UI----Thread、Handler、Looper、TimerTask等
- Android异步更新UI的方式之使用Handler的post(Runnabel r)方法
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Android Jni中使用线程及回调更新UI
- Android的线程使用来更新UI----Thread、Handler、Looper、TimerTask等
- Java 中Timer和TimerTask 定时器和定时任务使用的例子
- Android开发之——子线程中使用Toast或者更新UI