간단한 데모 응용 프로그램에서 위도와 경도를 얻기 위해 다음 링크에서 Fedor가 제공 한 코드를 사용합니다. 그 링크에서 제공 한 MyLocation 클래스를 사용하여 위도와 경도를 가져 오려고합니다.스레드가 완료되기 전에 진행 대화 상자가 닫히기 시작합니다 - Android
What is the simplest and most robust way to get the user's current location on Android?
나는 버튼 클릭에 위도와 경도를 가져보십시오. 버튼 클릭시 비동기 작업을 시작하고 위치 가져 오기 작업을 asynctask의 백그라운드 메서드에서 수행하도록 위임합니다.
사전 실행 - 진행 대화 상자가 시작되었습니다. 실행 후 - 진행률 대화 상자가 닫힙니다.
이것은 내 코드의 진행 대화 상자가 작동하는 방식이며 여기에 내가 가진 문제입니다. 진행 대화 상자가 올바르게 시작되지만 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가 완료되기도 전에 사라 무엇이 생각.
전문가 님,이 문제를 이해하고 해결할 수 있도록 도와주세요.
이와 관련하여 도움이 되시길 바랍니다.
기대, 최고 감사합니다, 그런 RONY
죄송합니다. 여기에 코드를 추가하는 방법을 볼 수 없으므로 여기에 직접 붙여 넣습니다. – user264953
최종 MyLocation myLocation = new MyLocation(); \t \t 최종 LocationResult locationResult = 새로운 LocationResult() { \t \t \t \t \t \t @Override \t \t \t 공개 무효 gotLocation (위치 위치) { \t \t \t \t // TODO 자동 생성 방법 스텁 \t \t \t \t \t \t \t} \t \t}; \t \t \t \t btn.setOnClickListener (새 OnClickListener를() { \t \t \t @Override \t \t \t 공공 무효 온 클릭 (보기 V) { \t \t \t \t myLocation.getLocation (LocationServices.this, locationResult); \t \t \t \t tv1.setText ("위도"); // 위도를 추가해야합니다. 여기에 있습니다. \t \t \t \t tv2.setText ("경도"); // 내가 이곳에 오기 경도를 추가 할 필요가 \t \t \t} \t \t}); – user264953
나는 당신의 제안에 따라 않았다. 그러나 loc.getlat()과 loc.getlon()을 사용하여 textview에 값을 설정하려면 어떻게해야합니까? 또한이 값을 설정하기 전에, 사용자가 작업 내에서 다른 작업을 수행 할 수 있도록해야합니다. 그렇게하지 못하게하고 위도/경도가 인출되는 동안 백그라운드에서 다른 구성 요소를 표시하려면 회 전자를 사용해야합니다. – user264953