1

我已经使用计时器在特定时间段后调用某些功能。

我已经用下面的代码做到了:

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

请帮我。

4

3 回答 3

2

定时器任务在不同的线程上运行。ui 应该在 ui 线程上更新。

所以你可以使用一个Handler. 或使用runOnUiThread

Handler m_handler;
Runnable m_handlerTask ;  
m_handler = new Handler();   
m_handlerTask = new Runnable()
{
  @Override 
  public void run() { 

    // do something  
    m_handler.postDelayed(m_handlerTask, 1000);    

  }
  };
 m_handlerTask.run();

另请将此作为旁注

用于计时器的 Android 线程

编辑:

您每次都在初始化您的视图。将其移至onCreate并将它们声明为类成员

  ListView lm=(ListView)findViewById(R.id.listView1);
  TextView tv = (TextView)findViewById(R.id.textView1);
  TextView tvNewMessages = (TextView)findViewById(R.id.tvNewMessages);
于 2013-08-27T11:45:52.827 回答
1

你必须使用这个:

Handler h = new Handler();
Runnable r = new Runnable(){

 @Override 
  public void run() { 

  //perform action here. 
   h.postDelayed(m_handlerTask, 1000);    

}
};
h.run();
于 2013-08-27T12:02:56.010 回答
1

或者你可以试试 CountDownTime

 new CountDownTimer(x, x) {

     public void onTick(long millisUntilFinished) {
            Toast.makeText(getApplicationContext(),"Insidedsfdsf Click  " , Toast.LENGTH_LONG).show();
     }

     public void onFinish() {
           UpdateTask();
     }
  }.start();
于 2013-08-27T12:03:21.470 回答