一个android Timer,为什么只执行了一次
1个回答
展开全部
public void getPaisong() {
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: {
// 初始化搜索模块,注册事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapMan, new MyMKSearchListener());
jiexi();
cou++;
Toast.makeText(
BMapApiDemoApp.mDemoApp.getApplicationContext(),
"执行了"+cou+"一次!", Toast.LENGTH_LONG).show();
}
break;
}
super.handleMessage(msg);
}
};
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer = new Timer(true);
timer.schedule(task, 5000); // 延时1000ms后执行,1000ms执行一次
// timer.cancel(); //退出计时器
}
我在oncreate方法里直接调用了getPaisong()方法,可是只显示cou=1
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1: {
// 初始化搜索模块,注册事件监听
mSearch = new MKSearch();
mSearch.init(app.mBMapMan, new MyMKSearchListener());
jiexi();
cou++;
Toast.makeText(
BMapApiDemoApp.mDemoApp.getApplicationContext(),
"执行了"+cou+"一次!", Toast.LENGTH_LONG).show();
}
break;
}
super.handleMessage(msg);
}
};
TimerTask task = new TimerTask() {
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer = new Timer(true);
timer.schedule(task, 5000); // 延时1000ms后执行,1000ms执行一次
// timer.cancel(); //退出计时器
}
我在oncreate方法里直接调用了getPaisong()方法,可是只显示cou=1
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询