我有一项活动。在此 Activity 中,我执行了 AsyncTask ,但它不起作用

发布于 2024-08-22 04:01:14 字数 2002 浏览 3 评论 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);
    }
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 
    }

}

Basically, my objective is:

  1. Constantly post the latitude/longitude to my website every minute or so.
  2. Of course, I want to do this in another thread, so it doesn't mess up my UI thread. This AsyncTask is supposed to solve that...but it's bringing up an error and I don't know why.

What can you do to accomplish my objective? It's very simple...just scan location and post to web every minute, in the background.

By the way, my onCreate method is like this. That's basically it.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-08-29 04:01:14

将其分为 3 个步骤:

  1. 使您想要的任何工作无需 AsyncTask
  2. 确保您已经弄清楚了 AsyncTask,在 doInBackground(... 中执行一个简单的 Log.e("haha", "gotcha") ) 并检查它是否显示
  3. 合并两者。

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

Break it into 3 steps:

  1. Make whatever you want to work work w/o AsyncTask
  2. Make sure you've figured out AsyncTask, do a simple Log.e("haha", "gotcha") in doInBackground(...) and check that it shows
  3. Combine the two.

For this case, I'd probably go with a Service triggered by AlarmManager. Guess you'll need the latter anyways.

若言繁花未落 2024-08-29 04:01:14

将其创建为服务。当您注册位置事件并采取适当行动时,无需使用计时器或警报管理器。

监听位置事件的示例可以在以下位置找到:
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);

Create this as a Service. No need to have a Timer or AlarmManager as you register for location events and act appropriately.

An example of listening to location events can be found at
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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文