0
private class ExecuteLocations extends AsyncTask<String, Void, Void>{ 
    private final ProgressDialog dialog = new ProgressDialog(ListProfiles.this); 

    protected void onPreExecute() { 
     //this.dialog.setMessage("Starting pre-execute..."); 
     //this.dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(String... arg0) { 
     check_profiles_lm=(LocationManager) ListProfiles.this.getSystemService(LOCATION_SERVICE); 
     myLocListen = new LocationListener(){ 
      @Override 
      public void onLocationChanged(Location location) { 
       HashMap params = new HashMap(); 
       params.put("lat", Double.toString(location.getLatitude())); 
       params.put("long", Double.toString(location.getLongitude())); 
       postData("http://mydomain.com",params); 

      } 
      @Override 
      public void onStatusChanged(String provider, int status,Bundle extras) { 
      } 
      @Override 
      public void onProviderDisabled(String provider) { 
      } 
      @Override 
      public void onProviderEnabled(String provider) { 
      } 
     };  
     check_profiles_lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 0, myLocListen); 
     return null; 
    } 

    protected void onPostExecute(final Void unused) { 
     if (this.dialog.isShowing()) { 
      //this.dialog.dismiss(); 
     } 
     //Do something else here 
    } 

} 

基本上,我的目标是:我有一个活动。在此活动中,我执行的AsyncTask,但它不工作

  1. 纬度/经度不断张贴到我的网站每分钟左右。
  2. 当然,我想在另一个线程中这样做,所以它不会弄乱我的UI线程。这个AsyncTask应该可以解决这个问题......但是它带来了一个错误,我不知道为什么。

你能做些什么来完成我的目标?这非常简单...只需扫描位置并在后台每分钟发布到网页。

顺便说一句,我的onCreate方法是这样的。基本上就是这样。

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
      new ExecuteLocations().execute(); 
      setContentView(R.layout.main_list); 
    } 

回答

1

打破它分为3个步骤:

  1. 做出力所能及的,你想工作工作W/O的AsyncTask
  2. 确保你已经想通了的AsyncTask,做一个简单的Log.e(” haha“,”gotcha“)在doInBackground(...)并检查它是否显示
  3. 合并这两者。

对于这种情况下,我可能会去与触发Service。无论如何你会需要后者。

0

创建此即服务。当您注册位置事件并采取适当行动时,无需拥有定时器或AlarmManager。

聆听位置的事件的例子可以在 http://code.google.com/p/android-bluetooth-on-motion/source/browse/#svn/trunk/BluetoothOnMotion/src/com/elsewhat

 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_NETWORK, MIN_DISTANCE_NETWORK,locationListener); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_GPS, MIN_DISTANCE_GPS,locationListener); 
找到
相关问题