GPS を使用してユーザーの現在の国を特定するアプリケーションを構築しています。現在、GPS は場所を見つけるのに時間がかかるため、検索中に ProgressDialog を表示したいと考えています。
したがって、LocationListener 内で、呼び出す前に宣言した ProgressDialog を呼び出しました
pd
そして、それが示す部分のすぐ下に、GPS を行うためのスレッドを作成しました。何らかの理由で、ProgressDialog が表示されません:/
これが私のコードです:
pd = ProgressDialog.show(TipCalculatorActivity.this, "", "Looking for GPS satellites...");
new Thread()
{
public void run()
{
try
{
Geocoder geocoder = new Geocoder(TipCalculatorActivity.this, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
countryCode = addresses.get(0).getAddressLine(2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
finish();
}
if(countryCode==null){
Toast.makeText(TipCalculatorActivity.this, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
finish();
}
Toast.makeText(TipCalculatorActivity.this, countryCode, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progressdialog
pd.dismiss();
}
}.start();
ここで何が問題なのですか?!
ありがとう!
編集
私の新しいコード: AsyncTask を呼び出す場所:
LocationListener onLocationChange=new LocationListener() {
public void onLocationChanged(Location loc) {
//sets and displays the lat/long when a location is provided
lat = loc.getLatitude();
lng = loc.getLongitude();
MyAsyncTask task = new MyAsyncTask(TipCalculatorActivity.this, lng, lat);
task.execute();
Toast.makeText(TipCalculatorActivity.this, "Done :D", Toast.LENGTH_LONG).show();
}
AsyncTask:
public class MyAsyncTask extends AsyncTask<Void, Void, Result>{
private Activity activity;
private ProgressDialog progressDialog;
private double longitude;
private double latitude;
private String countryCode;
public MyAsyncTask(Activity activity, double longitude, double latitude) {
super();
this.activity = activity;
this.longitude = longitude;
this.latitude = latitude;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog.show(activity, "", "Looking for GPS satellites...");
}
@Override
protected Result doInBackground(Void... v) {
Geocoder geocoder = new Geocoder(activity, Locale.getDefault());
List<Address> addresses;
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
countryCode = addresses.get(0).getAddressLine(2);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
if(countryCode==null){
Toast.makeText(activity, "Location is not available, please try again later.", Toast.LENGTH_LONG).show();
}
Toast.makeText(activity, countryCode, Toast.LENGTH_LONG).show();
return null;
}
@Override
protected void onPostExecute(Result result) {
progressDialog.dismiss();
Toast.makeText(activity.getApplicationContext(), "Finished.",
Toast.LENGTH_LONG).show();
}
}