2013-08-02 20 views
2

我是新來的Android所以這個問題可能是容易的,但在這裏不言而喻反正:上設置的時間間隔運行的AsyncTask

我想提出一個Android應用程序,將發送用戶的位置到遠程數據庫具有的AsyncTask 。現在它發生在你點擊一個按鈕時,但我希望它以設定的時間間隔自動發生。

我試圖做一個處理程序和「scheduleAtFixedRate」,但它不適合我;可能只是因爲我做錯了。這是我的代碼:

package com.paul.locations; 

import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Calendar; 
import java.util.List; 
import java.util.Timer; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.SystemClock; 
import android.preference.PreferenceManager; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.TextView; 
import android.widget.Toast; 

public class OptionsPage extends Activity implements OnClickListener { 

private String lat="", lon="", current = ""; 
private Button bUpdate, bGoogleMap, Confirm; 
private EditText TimerInterval; 
private TextView Latit, Longit; 
final Handler handler = new Handler(); 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "message"; 
private static final String UPDATE_INFO_URL = "http://192.168.1.16/locations/update_info.php"; 
// Progress Dialog 
private ProgressDialog pDialog; 

// JSON parser class`enter code here` 
JSONParser jsonParser = new JSONParser(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.options); 

    TimerInterval = (EditText) findViewById(R.id.etTimer); 
    Longit = (TextView) findViewById(R.id.tvLon); 
    Latit = (TextView) findViewById(R.id.tvLat); 
    bUpdate = (Button)findViewById(R.id.bUpdate); 
    bUpdate.setOnClickListener(this); 
    bGoogleMap = (Button)findViewById(R.id.bGoogleMap); 
    bGoogleMap.setOnClickListener(this); 
    Confirm = (Button)findViewById(R.id.bConfirm); 
    Confirm.setOnClickListener(this); 


      //This is the new line for timer task 
TimerTask task = new TimerTask(){ 
     @Override 
     public void run(){ 
      new AttemptUpdate().execute(); 
     } 

    }; 

    new Timer().scheduleAtFixedRate(task,0,5000); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.bGoogleMap: 
     Intent i = new Intent(OptionsPage.this, Location.class); 
     finish(); 
     startActivity(i); 
     break; 
    case R.id.bUpdate: 
     LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     android.location.Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     lon = Double.toString(location.getLongitude()); 
     lat = Double.toString(location.getLatitude()); 
     System.out.println(lon + ", " + lat); 
     Calendar c = Calendar.getInstance(); 
     SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
     current = df.format(c.getTime()); 
     System.out.println(current); 
     new AttemptUpdate().execute(); 
     break; 
    case R.id.bConfirm: 
     String Interval = TimerInterval.getText().toString(); 
     int I = Integer.parseInt(Interval); 
     System.out.println(I); 
     break; 
    } 
} 

class AttemptUpdate extends AsyncTask<String, String, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(OptionsPage.this); 
     pDialog.setMessage("Attempting to store your location..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(final String... args) { 
     // TODO Auto-generated method stub 
     String success; 
     SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(OptionsPage.this); 
     String post_username = sp.getString("username", "anon"); 

     try { 
      // Building Parameters 
      List<NameValuePair> params1 = new ArrayList<NameValuePair>(); 
      params1.add(new BasicNameValuePair("username", post_username)); 
      params1.add(new BasicNameValuePair("lat", lat)); 
      params1.add(new BasicNameValuePair("lon", lon)); 
      params1.add(new BasicNameValuePair("time", current)); 

      Log.d("request!", "starting"); 

      //Posting user data to script 
      JSONObject json = jsonParser.makeHttpRequest(
        UPDATE_INFO_URL, "POST", params1); 

      // full json response 
      Log.d("Store location attempt", json.toString()); 

      // json success element 
      success = json.getString(TAG_SUCCESS); 
      if(success.equals("SUCCESS")) { 
       Log.d("Location Stored!", json.toString());  
       finish(); 
       return json.getString(TAG_MESSAGE); 
      }else{ 
       Log.d("Storing Location Failure!", json.getString(TAG_MESSAGE)); 
       return json.getString(TAG_MESSAGE); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     }   
     return null; 
    } 

    protected void onPostExecute(String file_url) { 
     // dismiss the dialog once product deleted 
     pDialog.dismiss(); 
     if (file_url != null){ 
      Toast.makeText(OptionsPage.this, file_url, Toast.LENGTH_LONG).show(); 
     } 
     Latit.setText("Latitude currently is: " + lat); 
     Longit.setText("Longitude currently is: " + lon); 
    } 
} 
} 
中可以看到onCreate方法,我試圖讓定時器運行每5秒AttemptUpdate

,但它在任何幫助,將不勝感激鍵入時作爲錯誤出現!

添加計時器任務和計時器後,我運行代碼。該應用程序崩潰,我得到了下面的logcat錯誤:

08-02 13:50:02.844: W/dalvikvm(29346): threadid=13: thread exiting with uncaught exception (group=0x41b11438) 
08-02 13:50:02.854: E/AndroidRuntime(29346): FATAL EXCEPTION: Timer-0 
08-02 13:50:02.854: E/AndroidRuntime(29346): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.os.Handler.<init>(Handler.java:121) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.app.Dialog.<init>(Dialog.java:107) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.app.AlertDialog.<init>(AlertDialog.java:114) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.app.AlertDialog.<init>(AlertDialog.java:98) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.app.ProgressDialog.<init>(ProgressDialog.java:77) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at com.paul.locations.OptionsPage$AttemptUpdate.onPreExecute(OptionsPage.java:108) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at android.os.AsyncTask.execute(AsyncTask.java:534) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at com.paul.locations.OptionsPage$1.run(OptionsPage.java:69) 
08-02 13:50:02.854: E/AndroidRuntime(29346): at java.util.Timer$TimerImpl.run(Timer.java:284) 

回答

4

替換錯誤符合以下代碼:

new Timer().scheduleAtFixedRate(new TimerTask() { 
      @Override 
      public void run() { 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         new AttemptUpdate().execute();  
        } 
       }); 
      } 
     }, 0, 5000); 
+0

你是最偉大的,目標,它的作品! – Paul

4

你把的AsyncTask其中一個計時器任務應該去。看看這個例子TimerTask not executing?

TimerTask task = new TimerTask(){ 
    @Override 
    public void run(){ 
     new AttemptUpdate().execute(); 
    } 

} 

new Timer().scheduleAtFixedRate(task,0,5000); 
+0

所以不是延長我應該延長的TimerTask的AsyncTask的?那是唯一必要的改變嗎? – Paul

+0

不,你需要創建一個時間任務,在那個計時器任務中,你重寫'run'方法,然後這個asynctask進入那裏。看到我的更新 – tyczj

+0

我能夠運行代碼,但應用程序崩潰。我編輯了我的問題以包含新代碼以及logcat輸出。 – Paul