1
我一直在研究一個android應用程序,它定期檢查使用JSON的mysql數據庫,並且一切工作正常,我的代碼。在定時器上運行新線程Android
即時運行這個作爲一個計時器,因爲它只運行一次,然後停止。 我設法讓工作的唯一代碼在凍結的UI線程上運行http請求。 任何幫助將不勝感激。 預先感謝,
@Override
protected void onCreate(Bundle savedInstanceState) {
...
checkUpdate.start();
...
}
private Thread checkUpdate = new Thread() {
public void run() {
try {
// my code here to get web request to return json string
}
String response = httpclient.execute(httppost, responseHandler);
mHandler.post(showUpdate);
}
...
}
private Runnable showUpdate = new Runnable(){
public void run(){
try{
// my code here handles json string as i need it
Toast.makeText(MainActivity.this,"New Job Received...", Toast.LENGTH_LONG).show();
showja();
}
}
}
private void showja(){
Intent i = new Intent(this, JobAward.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
您的代碼沒有正確縮進......讓我們真的很難閱讀。 –
你的文章缺少很多信息(例如,什麼是'mHandler'變量,什麼類是'onCreate'方法,哪裏是具有計時器的代碼等) –
爲什麼不使用asynctask for這個目的 – Raghunandan