【问题标题】:I have one Activity. In this Activity, I executed a AsyncTask , but it's not working我有一个活动。在此活动中,我执行了 AsyncTask ,但它不起作用
【发布时间】:2010-02-18 02:36:25
【问题描述】:
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);
    }

【问题讨论】:

    标签: java android geolocation android-activity android-asynctask


    【解决方案1】:

    分为三个步骤:

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

    对于这种情况,我可能会选择由AlarmManager 触发的Service。猜猜你还是需要后者。

    【讨论】:

      【解决方案2】:

      将此创建为服务。当您注册位置事件并采取适当行动时,无需使用 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多