如何将字符串结果导入另一个类

时间:2013-05-12 14:39:28

标签: android string location

我一直在寻找使用以下代码的正确方法,该代码位于我的MainActivity中,并将结果输入到另一个活动中,因此我可以调用基于“http://www.website.com?lat:lng:speed:bearing”的网址价值通过。

locationCheck()位于我的Mainactivity中我如何拥有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) {

            }
        }
    };

1 个答案:

答案 0 :(得分:0)

以下内容允许我运行位置活动并将其结果作为字符串发布到全局可访问的表单中,然后允许我以通过其他活动创建上下文感知URL的格式使用...

正如您所看到的,toast使用全局值,这使我可以在可访问的地方测试它们。

在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) {

        }
    }
};

在Globals活动中

// 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;

}