私のシンプルなデモアプリケーションから緯度と経度を取得するには、以下のリンクでFedorが提供するコードを使用します。私はそのリンクで彼が提供するMyLocationクラスを使用して緯度と経度を取得しようとしています。スレッドが終了する前に進捗ダイアログが表示されなくなる - Android
What is the simplest and most robust way to get the user's current location on Android?
私は、ボタンをクリックしたときに、緯度と経度を取得してみてください。ボタンをクリックすると、私は非同期タスクを開始し、ロケーションフェッチ作業を、asynctaskのバックグラウンドメソッドのdoに委譲します。
事前実行 - 進行ダイアログが開始されました。実行後 - 進捗ダイアログが閉じられました。
これは、コードの進捗状況のダイアログボックスが機能する方法です。ここに私の問題があります。進行状況ダイアログは正しく開始されますが、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が終了する前であっても消える作るものの考え方。
この問題を理解して解決するのを手伝ってください。
この点に関する助けがあれば助かります。今後
、 よろしく、 ロニー
申し訳ありませんが、私はここにコードを追加する方法を見ることができないので、私は直接それをここに貼り付けています。.. – user264953
最終的なMyLocationのmyLocation =新しい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ます。public void onClickの(ビューV){ \t \t \t \t myLocation.getLocation(LocationServices.this、locationResult)。 \t \t \t \t tv1.setText( "latitude"); //緯度を追加する必要があります。ここに移動します。 \t \t \t \t tv2.setText( "経度"); //私はここに来る経度を追加する必要が \t \t \t} \t \t}); – user264953
私はあなたの提案にしたがっていましたが、loc.getlat()とloc.getlon()を使って、私が持っているtextviewに値を設定するにはどうしたらいいですか...これらの値を設定する前に、ユーザーがアクティビティ内で他のアクションを実行するようにしています。そのようにすることをブロックし、他のコンポーネントをバックグラウンドで表示するには、long、longをフェッチします。 – user264953