进度对话在线程完成之前被解雇 - Android

时间:2010-12-28 06:22:20

标签: android android-widget

我在以下链接中使用Fedor提供的代码,以便从我的简单演示应用程序中获取经度和纬度。我正在尝试使用他在该链接中提供的MyLocation类来获取经度和纬度。

What is the simplest and most robust way to get the user's current location on Android?

我尝试按下按钮获取经度和纬度。在按钮单击时,我启动异步任务并将位置提取工作委托给我的asynctask的do in background方法。

预执行 - progressdialog已启动。 后执行 - 进度对话被驳回。

这是如何,我的代码中的进度对话框应该工作,这是我的问题。正确启动了进度对话框,但即使在doinbackground方法中打印纬度和经度之前,进度对话框也会被取消。

我不明白为什么会这样。

这是我的前端活动

public class LocationServices extends Activity {
MyLocation myLocation = new MyLocation();
LocationResult locationResult;
TextView tv1, tv2;
Location location;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv1 = (TextView) findViewById(R.id.tv1);
    tv2 = (TextView) findViewById(R.id.tv2);


    Button btn = (Button) findViewById(R.id.Button01);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            new LocationAsyncTasking().execute();

        }
    });

}

public class LocationAsyncTasking extends AsyncTask<String, Void, Void> {

    ProgressDialog dialog;
    int totalAvail;

    protected void onPreExecute() {

        // this.dialog.setMessage("Inserting data...");
        dialog = new ProgressDialog(LocationServices.this);
        this.dialog.setMessage("Fetching data...");
        this.dialog.show();
    }

    protected Void doInBackground(String... args) {
        Looper.prepare();

        locationResult = new LocationResult() {

            public void gotLocation(Location location) {
                // TODO Auto-generated method stub

                // LocationServices.this.location = location;
                System.out.println("Progress dialog should be present now - latitude"+location.getLatitude());
                System.out.println("Progress dialog should be present now - longitude"+location.getLongitude());
            }
        };
        myLocation.getLocation(LocationServices.this, locationResult);

        return (null);
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }

}

}

我很困惑,想到即使在doinbackground中的SOP完成之前,这个进度对话框的消失也会消失。

专家,请帮助我理解并解决这个问题。

非常感谢这方面的任何帮助。

展望未来, 最好的祝福, 罗尼

2 个答案:

答案 0 :(得分:1)

我发现所有的例子都让人很困惑是最新的Android编程,最终...最简单的是那个有效的,所以这个答案是基于上面的注释,其中的代码粘贴在其中,但我已经附加了我的完整活动,其中包括按下按钮后标签的位置更新。认为这会让某人的生活更轻松。哦,并确保你使用上面提到的MyLocation类。

package com.benderapp;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.benderapp.MyLocation.LocationResult;

public class DefaultActivity extends Activity {
    MyLocation myLocation = new MyLocation();
    TextView tv1, tv2;
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
    btn = (Button)findViewById(R.id.Button01);

    final MyLocation myLocation = new MyLocation();
        final LocationResult locationResult = new LocationResult() {

            @Override
            public void gotLocation(Location location) {
                    // TODO Auto-generated method stub
                    tv1.setText(location.getLatitude() + "");//need to add latitude i get here
                    tv2.setText(location.getLongitude() + "");//need to add longitude i get here                       
            }
    };


    btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                    myLocation.getLocation(DefaultActivity.this,locationResult);
            }
        });     
    }
}

答案 1 :(得分:0)

这样的事情。如果需要,只需添加ProgressDialog。

public class LocationServices extends Activity {
    MyLocation myLocation = new MyLocation();
    TextView tv1, tv2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);

        Button btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                myLocation.getLocation(LocationServices.this, locationResult);
            }
        });

    }

    public LocationResult locationResult = new LocationResult() {
            public void gotLocation(Location location) {
                runOnUiThread(new Runnable(){
                    @Override
                    public void run(){
                        tv1.setText(location.getLatitude());
                        tv2.setText(location.getLongitude());
                    }
                });
            }
        };
}