我正在组织应用程序,我需要一些帮助将GPS坐标发送到另一个类,该类将它们发送到数据库。当按下Main活动中的send按钮时,它将启动一个意图,该意图开始LocationActivity。从那里,我想将GPS坐标发送到SendActivity,后者将它们发送到数据库(通过PHP程序)。如何将坐标发送到SendActivity?我评论了我尝试过的部分,但没有用。任何帮助,将不胜感激。谢谢!
LocationActivity.java:
public class LocationActivity extends Activity{
private LocationManager locManager;
private LocationListener locListener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startLocation();
}
private void startLocation()
{
//get a reference to the LocationManager
locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//checked to receive updates from the position
locListener = new LocationListener() {
public void onLocationChanged(Location location) {
SendActivity.send(location); //What to do here?
}
public void onProviderDisabled(String provider){
//labelState.setText("Provider OFF");
}
public void onProviderEnabled(String provider){
//labelState.setText("Provider ON ");
}
public void onStatusChanged(String provider, int status, Bundle extras){
//Log.i("", "Provider Status: " + status);
}
};
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
}
}
SendActivity.java:
public class SendActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
void send(Location loc)
{
Log.i("", String.valueOf(loc.getLatitude() + " - " + String.valueOf(loc.getLongitude())));
String lat = String.valueOf(loc.getLatitude());
String lon = String.valueOf(loc.getLongitude());
SharedPreferences shared = getSharedPreferences("PEOPLE_PREFERENCES", MODE_PRIVATE);
final String usr_id2 = shared.getString("USR_ID", "none");
if (lat != "0" && lon != "0")
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/test/example.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("lat", lat));
nameValuePairs.add(new BasicNameValuePair("lon", lon));
nameValuePairs.add(new BasicNameValuePair("id", usr_id2));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
}
catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
}
最佳答案
无需创建活动即可获取LatLong ..
以下是获取LatLong的课程:
class UpdateGeoLocation {
LocationManager locationManager;
String provider;
double lat, lng;
Location location;
Location returnlocation;
Context context;
public static String locationdetails = null;
public UpdateGeoLocation(Context context) {
this.context = context;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
locationManager = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
returnlocation = locationManager.getLastKnownLocation(provider);
}
public Location getLocation(Location location) {
if (location != null) {
Log.d("one", "IF getLocation :: " + location);
returnlocation.setLatitude(location.getLatitude());
returnlocation.setLongitude(location.getLongitude());
} else {
try {
listenForLocation(provider);
} catch (NullPointerException nullPointerException) {
nullPointerException.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return returnlocation;
}
private void listenForLocation(String providerName) {
locationManager.requestLocationUpdates(providerName, 0, 0,
locationListener);
if (location != null) {
location.getLatitude();
location.getLongitude();
getLocation(location);
} else {
// Toast.makeText(context, "location null",
// Toast.LENGTH_SHORT).show();
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
returnlocation.setLatitude(location.getLatitude());
returnlocation.setLongitude(location.getLongitude());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
Following is the Activity where I used above class to get LatLong :
Location location = null;
OnCreate() :
try {
Looper.prepare();
if (null != (location = requestForLocation())) {
if (null != location) {
Log.d("one", "Location :: " + location.getLatitude());
Log.d("one", "Location LATLONG :: " + location.getLongitude());
//Use intent or bundle here to send LatLong to other activities..
} else {
Log.d("one", "Location not found:: ");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
private Location requestForLocation() {
return new UpdateGeoLocation(MonsterLoginActivity.this)
.getLocation(null);
}
关于java - 如何将GPS坐标发送到另一类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9478570/