2012-02-28 91 views
1

我正在组织我的应用程序,我需要一些帮助将GPS坐标发送到另一个将它们发送到数据库的类。当主活动中的发送按钮被按下时,它启动一个开始LocationActivity的意图。从那里,我想将GPS坐标发送到SendActivity,SendActivity将它们发送到数据库(通过PHP程序)。如何将坐标发送到SendActivity?我评论了我尝试过的一个部分,但它不起作用。任何帮助,将不胜感激。谢谢!如何将GPS坐标发送到另一个类

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 
     } 

     } 
    } 
} 
+0

你在做没有意义。 Plz认为只是以“常规”的方式保存数据库,而不是通过其他活动。 – Warpzit 2012-02-28 08:47:41

回答

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); 
} 
0

你真的打算让SendActivity是一个全新的活动,或者你只是想一个类来实现一个发送功能?如果是后者,你可以将它定为普通班级,而不是一个活动。

如果您希望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"); 
} 

但它没有任何意义,我认为只是调用另一个活动中的功能。

+0

那么我会怎么做一个类呢?我想现在,但我不知道如何从主要活动开始startLocation()。 – mkyong 2012-02-28 09:28:32

0

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

1)通过广播接收器发送。 2)将它保存在一些变量/首选项中,然后从另一个活动中读取它。

3)实现一个Handler并通过Message /发送消息。

4)通过intents/put extra。

和很多其他方式。

相关问题