我已经使用计时器在特定时间段后调用某些功能。
我已经用下面的代码做到了:
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
public void run() {
Toast.makeText(getApplicationContext(),"Insidedsfdsf Click " , Toast.LENGTH_LONG).show();
UpdateTask();
}
}, 1, 1000);
public void UpdateTask() {
// TODO Auto-generated method stub
try
{
Toast.makeText(getApplicationContext(),"update " , Toast.LENGTH_LONG).show();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String id = intent.getStringExtra(MainActivity.EXTRA_ID);
String[] lst = null;
String[] lstNew = null;
ListView lm=(ListView)findViewById(R.id.listView1);
TextView tv = (TextView)findViewById(R.id.textView1);
TextView tvNewMessages = (TextView)findViewById(R.id.tvNewMessages);
tv.setText("Welcome " + message);
CallSoap cs=new CallSoap();
lst=cs.GetMessage(id);
lstNew=cs.GetNewMessage(id);
final int numOfMessages=lstNew.length;
//Toast.makeText(getApplicationContext(),"Call "+lstNew.length , Toast.LENGTH_LONG).show();
tvNewMessages.setText("You have "+ numOfMessages +" new messages");
ArrayAdapter<String> adpt = new ArrayAdapter<String>(Messages.this, android.R.layout.simple_list_item_1,lst){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if(position<numOfMessages){
v.setBackgroundColor(Color.RED);
}
else
{
v.setBackgroundColor(Color.WHITE);
}
return v;
}
};
lm.setAdapter(adpt);
}
catch(Exception ex)
{
ex.printStackTrace();
//Toast.makeText(getApplicationContext(),"Call "+ex.getMessage() , Toast.LENGTH_LONG).show();
}
}
但我发现我的 UpdateTask 函数没有被调用,并且在 logcat 中它向我显示错误:
can't create handler inside thread
请帮我。