2013-05-12 17 views
-5

我一直在尋找正確的方式來處理MainActivity中的以下代碼,並將結果提交到另一個活動,因此我可以調用URL'http://www.website.com?lat:lng:speed:bearing'這將基於所通過的值。如何將字符串結果拉到另一個類

locationCheck()坐在我的主要活動中,我有一個JSONParse活動,我想使用找到的位置來構造url。我不知道如何分享結果。

在HTML或PHP中,我會使用全局值。

儘管我可以打電話和烤麪包,但我還沒有找到分享結果的最佳方式。

public void locationCheck() { 
     MyLocation myLocation = new MyLocation();  
     // this is the result 
     myLocation.getLocation(this, locationResult); 
    } 

    public LocationResult locationResult = new LocationResult() { 
     public void gotLocation(final Location location) { 
      try { 
       double lat = location.getLatitude(); 
       double lng = location.getLongitude(); 
       if (lat != 0.0 && lng != 0.0) { 

        String sLat; 
        String sLng; 
        sLat = Double.toString(lat); 
        sLng = Double.toString(lng); 
        String gps_location = sLat + " : " + sLng; 
        Toast.makeText(getBaseContext(), 
          "We got gps location! " + gps_location + "", 
          Toast.LENGTH_LONG).show(); 
       } 
      } catch (Exception e) { 

      } 
     } 
    }; 
+0

,你的問題是什麼? – Blackbelt 2013-05-12 14:41:07

+0

「in to another class」有點含糊 – 2013-05-12 14:42:50

+0

對不起 - 我寫了課的地方,我的意思是活動....重寫。 – 2013-05-12 14:48:34

回答

0

下面讓我有一個位置活動的運行,並張貼其結果作爲字符串全局可訪問的形式,然後讓我創建通過另一活動上下文感知URL的格式使用...

正如你所看到的敬酒使用的全球價值,讓我測試他們在哪裏可以訪問。

在MainActivity ....

public void locationCheck() { 
    MyLocation myLocation = new MyLocation();  
    // this is the result 
    myLocation.getLocation(this, locationResult); 
} 

public LocationResult locationResult = new LocationResult() { 
    public void gotLocation(final Location location) { 
     try { 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      double bearing = location.getBearing(); 
      double speed = location.getSpeed(); 
      if (lat != 0.0 && lng != 0.0) { 

       String sLat; 
       String sLng; 
       String sBearing; 
       String sSpeed; 

       sLat = Double.toString(lat); 
       sLng = Double.toString(lng); 
       sBearing = Double.toString(bearing); 
       sSpeed = Double.toString(speed); 

       // send to global vars 

       Globals.glat = sLat; 
       Globals.glng = sLng; 
       Globals.gBearing = sBearing; 
       Globals.gSpeed = sSpeed;  

       String gps_location = Globals.glat + " : " + Globals.glng; 

       @SuppressWarnings("unused") 
       String gps_location2 = sBearing + " : " + sSpeed; 

       Toast.makeText(getBaseContext(), 
         "We got gps location! " + gps_location + "", 
         Toast.LENGTH_LONG).show(); 
      } 
     } catch (Exception e) { 

     } 
    } 
}; 

在全局活動

// Global Values 
// http://stackoverflow.com/questions/3100726/sharing-global-variables-across-activities-problem-when-using-a-webview 
// http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables?rq=1 

    public class Globals { 

    static String glat; 
    static String glng; 
    static String gBearing; 
    static String gSpeed; 

} 
相關問題