2010-12-28 5 views
0

간단한 데모 응용 프로그램에서 위도와 경도를 얻기 위해 다음 링크에서 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

답변

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()); 
        } 
       }); 
      } 
     }; 
} 
+0

죄송합니다. 여기에 코드를 추가하는 방법을 볼 수 없으므로 여기에 직접 붙여 넣습니다. – user264953

+0

최종 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

+0

나는 당신의 제안에 따라 않았다. 그러나 loc.getlat()과 loc.getlon()을 사용하여 textview에 값을 설정하려면 어떻게해야합니까? 또한이 값을 설정하기 전에, 사용자가 작업 내에서 다른 작업을 수행 할 수 있도록해야합니다. 그렇게하지 못하게하고 위도/경도가 인출되는 동안 백그라운드에서 다른 구성 요소를 표시하려면 회 전자를 사용해야합니다. – user264953

1

나는 일 하나가 안드로이드 프로그래밍에 새로운, 결국 ... 간단한이 된 것을 오히려 혼란 모든 예제를 발견, 그래서이 답변은 그 위의 의견을 기반으로 붙여 넣은 코드가 그것,하지만 난 내 모든 활동을 첨부했습니다 .. 당신이 버튼을 누르면 레이블이 위치로 업데이 트됩니다. 누군가의 삶을 편하게 해줄 것이라고 생각했습니다. 아, 위에서 언급 한 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); 
      } 
     });  
    } 
} 
관련 문제