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线。这个际关的是应该以解决那个...但它带来了一个错误我不知道为什么。

你能做些什么来完成我的目标?这是非常简单的是...只是扫描的位置,并张贴到网的每一分钟,在背景中。

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

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            new ExecuteLocations().execute();
            setContentView(R.layout.main_list);
    }
有帮助吗?

解决方案

把它分成3个步骤:

  1. 做任何你想要工作的工作w/o际关
  2. 确保你已经想出际关,做一个简单的记录。e("哈哈","疑难杂症")在 doInBackground(...) 和检查,它显示了
  3. 把两者结合起来。

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

其他提示

创建这样一个服务。没有必要有一个定时器或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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top