0
在我的應用程序中,我有一個對話框。點擊是時,它發送一個以緯度和經度爲參數的HTTP請求。截至目前,該應用程序從GPS_PROVIDER中檢索這些信息,但如果GPS不可用,我希望它能從3G和WiFi獲取座標。 有人可以幫忙嗎?從3G/WiFi獲取座標
AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Dialog title")
.setMessage("Do you want to send a request?")
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER) && location!=null) {
//do HTTP request stuff
if (isOnline()) {
if (response.getStatusLine().getStatusCode()==200) {
Toast toast = Toast.makeText(getActivity(), "Request has been sent. Your coordinates are: " + Double.toString(location.getLatitude()) + " (Latitude) " + Double.toString(location.getLongitude()) + " (Longitude).", Toast.LENGTH_LONG);
toast.show();
}
else {
Toast toast = Toast.makeText(getActivity(), "Request has NOT been sent.", Toast.LENGTH_LONG);
toast.show();
}
}
else {
Toast toast = Toast.makeText(getActivity(), "Network is not available.", Toast.LENGTH_LONG);
toast.show();
}
}
else {
Toast toast = Toast.makeText(getActivity(), "GPS is not available.", Toast.LENGTH_LONG);
toast.show();
}
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast toast = Toast.makeText(getActivity(), "Request has not been sent.", Toast.LENGTH_LONG);
toast.show();
}
}).create();
ad.show();
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null &&
cm.getActiveNetworkInfo().isConnectedOrConnecting();
}
謝謝!很有幫助。 – user3673449 2014-10-08 13:54:13