2010-12-28 9 views
0

私のシンプルなデモアプリケーションから緯度と経度を取得するには、以下のリンクで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が終了する前であっても消える作るものの考え方。

この問題を理解して解決するのを手伝ってください。

この点に関する助けがあれば助かります。今後

、 よろしく、 ロニー

答えて

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 =新しい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

+0

私はあなたの提案にしたがっていましたが、loc.getlat()とloc.getlon()を使って、私が持っているtextviewに値を設定するにはどうしたらいいですか...これらの値を設定する前に、ユーザーがアクティビティ内で他のアクションを実行するようにしています。そのようにすることをブロックし、他のコンポーネントをバックグラウンドで表示するには、long、longをフェッチします。 – user264953

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); 
      } 
     });  
    } 
} 
関連する問題