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 
    }

}

基本上,我的目标是:

  1. 每分钟左右不断将纬度/经度发布到我的网站。
  2. 当然,我想在另一个线程中执行此操作,所以它不会弄乱我的 UI 线程。这个 AsyncTask 应该可以解决这个问题……但它引发了一个错误,我不知道为什么。

你能做些什么来实现我的目标?这非常简单……只需在后台每分钟扫描一次位置并发布到网络上。

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

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

2 回答 2

1

把它分成3个步骤:

  1. 让任何你想工作的东西都可以在没有 AsyncTask 的情况下工作
  2. 确保你已经找到了 AsyncTask,执行一个简单的 Log.e("haha", "gotcha") indoInBackground(...)并检查它是否显示
  3. 将两者结合起来。

对于这种情况,我可能会选择由Service触发AlarmManager。猜猜你无论如何都需要后者。

于 2010-02-18T06:50:19.670 回答
0

将此创建为服务。当您注册位置事件并采取适当行动时,无需使用 Timer 或 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);
于 2010-02-18T07:34:06.353 回答