简体   繁体   English

传递价值另一个活动

[英]passing value another activity

I made a WeatherSyncAdapterClass, I create a method and get the latitude and longitude value from GPS. 我创建了WeatherSyncAdapterClass,创建了一个方法,并从GPS获取了纬度和经度值。

public void getGPS(){
    Context context = WeatherSyncAdapter.super.getContext();
    gps = new GpsTrack(context);
    if (gps.canGetLocation()) {
        while (lat == 0.0 && lon == 0.0) {
            setLat(gps.getLatitude());
            setLon(gps.getLongitude());
            latitude = Double.toString(lat);
            longitude = Double.toString(lon);
        }
    }
    else {
        gps.showSettingsAlert();
    }
}

...and then I set the URI value for the "locationsetting" with this statement: ...然后使用以下语句为“ locationsetting”设置URI值:

    public void setLatLon(double a, double b) {
    //Double.toString(weather.getLatitude()+weather.getLongitude())
    LatLon = Double.toString(a+b);
}

(I just set the uri for the location setting from addition latitude and longitude ^_^, I know this is stupid) (我只是从附加纬度和经度^ _ ^设置位置设置的uri,我知道这很愚蠢)

My question is how to pass the "LatLon" value to the "mainActivity" for matching the URI? 我的问题是如何将“ LatLon”值传递给“ mainActivity”以匹配URI? I know intent will pass the value but i think thats will be bad solution cause will (maybe) open the another activity. 我知道意图会传递该值,但我认为那将是不好的解决方案,因为(可能)会打开另一个活动。

please help me :((( 请帮我 :(((

Intent intent = new Intent(current.this, mainActivity.class);
intent.putExtra("Lat",Double.toString(a));
intent.putExtra("Lon",Double.toString(b));
startActivity(intent);

Using the intents you can pass values to other activities. 使用这些意图可以将值传递给其他活动。 intent.putExtra method/function will send data in form of key,value pair. intent.putExtra方法/函数将以键,值对的形式发送数据。

In the MainActivity.class you can use following code to retrive the data from intent MainActivity.class您可以使用以下代码从intent中检索数据

Intent intent = getIntent();
String latitude = intent.getStringExtra("Lat");
String longitude = intent.getStringExtra("Lon");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM