我試圖使用異步任務獲取當前位置此代碼工作正常,沒有異步任務,但是當我嘗試與異步任務它顯示沒有找到位置和對不起沒有地址found.here是我的代碼PLZ幫我在此先感謝使用異步任務獲取當前位置
public class MainActivity extends Activity {
/** Called when the activity is first created. */
String addressString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Getlocation().execute();
}
//起動異步任務
class Getlocation extends AsyncTask<String, String, String> {
ProgressDialog p;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
p = new ProgressDialog(MainActivity.this);
p.setMessage("Please Wait Retrieving data..");
p.setTitle(" ");
p.setIcon(R.drawable.settings2);
p.show();
}
@Override
protected String doInBackground(String... params) {
Looper.prepare();
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
//String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
Looper.loop();
return addressString;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
p.dismiss();
} }
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
addressString = "Sorry No Address Found";
if(location!=null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;
double lattitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List<Address> addresses= gc.getFromLocation(lattitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if(addresses.size()>0) {
Address address=addresses.get(0);
for(int i=0;i<address.getMaxAddressLineIndex();i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
Toast.makeText(getApplicationContext(), "Current Position is :\n"+ latLong + "\n"+ addressString ,Toast.LENGTH_LONG).show();
// myLocation.setText("Current Position is :\n"+ latLong + "\n"+ addressString);
}
}
//這裏是我的logcat
10-01 22:34:13.720: E/WindowManager(2868): Activity com.example.sc.MainActivity has leaked window [email protected] that was originally added here
10-01 22:34:13.720: E/WindowManager(2868): android.view.WindowLeaked: Activity com.example.sc.MainActivity has leaked window [email protected] that was originally added here
10-01 22:34:13.720: E/WindowManager(2868): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
10-01 22:34:13.720: E/WindowManager(2868): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
10-01 22:34:13.720: E/WindowManager(2868): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
10-01 22:34:13.720: E/WindowManager(2868): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
10-01 22:34:13.720: E/WindowManager(2868): at android.view.Window$LocalWindowManager.addView(Window.java:547)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.Dialog.show(Dialog.java:277)
10-01 22:34:13.720: E/WindowManager(2868): at com.example.sc.MainActivity$Getlocation.onPreExecute(MainActivity.java:68)
10-01 22:34:13.720: E/WindowManager(2868): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-01 22:34:13.720: E/WindowManager(2868): at android.os.AsyncTask.execute(AsyncTask.java:534)
10-01 22:34:13.720: E/WindowManager(2868): at com.example.sc.MainActivity.onCreate(MainActivity.java:51)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.Activity.performCreate(Activity.java:5008)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-01 22:34:13.720: E/WindowManager(2868): at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 22:34:13.720: E/WindowManager(2868): at android.os.Looper.loop(Looper.java:137)
10-01 22:34:13.720: E/WindowManager(2868): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-01 22:34:13.720: E/WindowManager(2868): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 22:34:13.720: E/WindowManager(2868): at java.lang.reflect.Method.invoke(Method.java:511)
10-01 22:34:13.720: E/WindowManager(2868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-01 22:34:13.720: E/WindowManager(2868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 22:34:13.720: E/WindowManager(2868): at dalvik.system.NativeStart.main(Native Method)
LogCat說什麼? –
您需要發佈LogCat錯誤以便任何人都能夠爲您提供幫助。我的第一個猜測是,你沒有添加在你的Android清單中使用位置的權限xml –
@ PaulD'Ambra我已經添加了所有需要的權限,它在工作我一般的主線程,它涉及到異步它不工作 –