2013-10-21 139 views
0

我想創建一個小型的android應用程序,它會在點擊一個按鈕(即設置活動)之後定期顯示系統時間...創建按鈕和設置週期的代碼通過意圖活動是這樣的:在android中執行週期性任務

package com.example.timeupdate; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

Button button; 
TextView show; 

@Override 
protected void onCreate(Bundle I_Love_Biriyani) { 
    super.onCreate(I_Love_Biriyani); 
    setContentView(R.layout.activity_main); 

    button = (Button) findViewById (R.id.pressButton); 
    show = (TextView) findViewById (R.id.Show); 

    button.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      Intent openTimeUpdater = new Intent("com.example.timeupdate.TIMEUPDATER"); 
      startActivity(openTimeUpdater); 

     } 
    }); 

} 


@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    finish(); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 



} 

這裏是代碼重複我曾經的TimerTask類來執行這項工作的計時器(比如說爲5秒):

package com.example.timeupdate; 

import java.util.Date; 
import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class TimeUpdater extends Activity { 

    TextView Show; 

    TimerTask timer= new TimerTask(){ 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      Date d = new Date(); 
      Show.setText(""+d); 
     } 

    }; 

    @Override 
    protected void onCreate(Bundle hotovaga) throws IllegalStateException { 
     // TODO Auto-generated method stub 
     super.onCreate(hotovaga); 
     setContentView(R.layout.new_update); 

     Show = (TextView) findViewById (R.id.time); 

     Timer t = new Timer(); 
     t.scheduleAtFixedRate(timer , 0 , 5000); 

    } 


} 

點擊按鈕後,時間只顯示一次,然後應用程序停止顯示對話消息。需要解釋以同樣的方式完成這項工作。

+0

你試圖達到什麼樣的任務。你可以使用計時器或處理程序。順便說一句,你有第二個活動中的兩個計時器對象 – Raghunandan

回答

1

您試圖訪問非UI線程內的UI元素。

Show.setText(""+d); 

相反,把它包起來runOnUiThread接口獲取正確的輸出。

使用下面的代碼爲您TimeUpdater

public class TimeUpdater extends Activity { 


    TextView Show = null; 
    Calendar c; 
    int seconds; 
    int minutes; 
    int hours; 

    TimerTask timer= new TimerTask(){ 

     @Override 
     public void run() { 
      c = Calendar.getInstance(); 
      seconds = c.get(Calendar.SECOND); 
      minutes = c.get(Calendar.MINUTE); 
      hours = c.get(Calendar.HOUR); 

      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        Show.setText(hours + ":" + minutes + ":" + seconds); 
       } 
      }); 
     } 

    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.new_update); 

     Show = (TextView) findViewById (R.id.time); 

     Timer t = new Timer(); 
     t.scheduleAtFixedRate(timer , 0 , 5000); 


    } 

} 
0

與runOnUiThread()結合使用的實際定時器(java.util.Timer中)是解決這個問題的一種方式,以下是如何一個例子執行它。

public class myActivity extends Activity { 

private Timer myTimer; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    myTimer = new Timer(); 
    myTimer.schedule(new TimerTask() { 
     @Override 
     public void run() { 
      TimerMethod(); 
     } 

    }, 0, 1000); 
} 

private void TimerMethod() 
{ 
    //This method is called directly by the timer 
    //and runs in the same thread as the timer. 

    //We call the method that will work with the UI 
    //through the runOnUiThread method. 
    this.runOnUiThread(Timer_Tick); 
} 

private Runnable Timer_Tick = new Runnable() { 
    public void run() { 

    //This method runs in the same thread as the UI. 
    // Set your textView data here.    

    //Do something to the UI thread here 

    } 
}; 
} 
0

使用Play-Service的PeriodicTask,它是Google安排工作背景的最新工具。

+0

這將節省讀者一些谷歌搜索,如果你包括一個鏈接到文檔:) – jmuet