如何将GPS坐标发送到另一个类

时间:2012-02-28 08:26:04

标签: java android class gps

我正在组织我的应用程序,我需要一些帮助将GPS协调员发送到另一个将它们发送到数据库的类。当按下主活动中的发送按钮时,它会启动一个开始LocationActivity的意图。从那里,我想将GPS坐标发送到SendActivity,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
         }

        }
    }
}

3 个答案:

答案 0 :(得分:0)

你真的打算让SendActivity成为一个全新的活动,还是只想让一个类实现一个Send函数?如果是后者,你可以把它变成一个普通的类而不是它成为一个活动。

如果您希望SendActivity成为新活动(使用新活动屏幕),则可以使用新意图启动它,并将gps坐标添加到意图包中。

这样的东西
public void onLocationChanged(Location location) {
   //SendActivity.send(location); //What to do here?
   Intent i = new Intent(this, SendActivity.class);
   float lat = ... //calculate lat and lon here
   float lon = ...
   i.putExtra("latitude", lat);
   i.putExtra("longitude", lon);
   this.startActivity(i);
}

在SendActivity中:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    float lat = getIntent().getFloatExtra("latitude");
    float lon = getIntent().getFloatExtra("longitude");
}

但是我认为只是在另一个活动中调用一个函数是没有意义的。

答案 1 :(得分:0)

有很多方法可以在课程之间发送位置。

1)通过广播接收方发送。

2)将其保存在某些变量/首选项中,并从另一个活动中读取它。

3)实现Handler并传递消息/发送消息。

4)通过意图/增加额外费用。

以及许多其他方式。

答案 2 :(得分:0)

无需创建活动来获取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);
}