Android 变量作用域;或者,我的绳子到底在哪里

发布于 2025-01-06 09:00:07 字数 1779 浏览 1 评论 0原文

我正在开发一个简单的 Android 应用程序,带有 GPS 监听器和网络视图。

我可以毫无问题地获取纬度和经度。问题是,我想将纬度和经度放入 URL 中(例如 myurl.com/mypage.php?lat=57&lon=21)...但是存储数据的变量仅限于其类。我不知道如何声明或创建可以在整个主类中使用的变量。这是我的代码:

public class WTest2Activity extends Activity {
    public String txt;
    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location loc) {
            loc.getLatitude();
            loc.getLongitude();
            txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
            Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();
        }
        public void onProviderDisabled(String provider) {
            Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
        }
        public void onProviderEnabled(String provider) {
            Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {} /* do nothing */

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* Use the LocationManager class to obtain GPS locations */
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        WebView webview;
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
    }
}

I'm working on a simple Android app, with a GPS listener and a webview.

I can get the latitude and longitude with no issue. The problem is, I want to put the latitude and longitude into a URL (like myurl.com/mypage.php?lat=57&lon=21)... but the variable the data is stored in is confined to its class. I can't figure out how to declare or create a variable that I can use throughout the entire main class. Here's my code:

public class WTest2Activity extends Activity {
    public String txt;
    public class MyLocationListener implements LocationListener {

        public void onLocationChanged(Location loc) {
            loc.getLatitude();
            loc.getLongitude();
            txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
            Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();
        }
        public void onProviderDisabled(String provider) {
            Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
        }
        public void onProviderEnabled(String provider) {
            Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
        }
        public void onStatusChanged(String provider, int status, Bundle extras) {} /* do nothing */

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /* Use the LocationManager class to obtain GPS locations */
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
        mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

        WebView webview;
        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

┾廆蒐ゝ 2025-01-13 09:00:07

您的 OnCreate 方法是在 Activity 启动时调用的方法。此时,txtnull。这就是为什么它不会显示在您的网址中。然后,onLocationChanged 设置了 txt 的值,但是之后您在哪里使用它呢? 无处

您应该将 onCreate 中的操作移至 onLocationChanged

public void onLocationChanged(Location loc) {
    loc.getLatitude();
    loc.getLongitude();
    txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
    Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();

    webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
}

Your OnCreate method is the method called at start of Activity. At that time, txt is null. That's why it doens't show in your url. Then, onLocationChanged sets the value of txt but where are you using this afterwards? nowhere!.

you should move what you do in onCreate to onLocationChanged:

public void onLocationChanged(Location loc) {
    loc.getLatitude();
    loc.getLongitude();
    txt = "Latitude: " + loc.getLatitude() + "Longitude: " + loc.getLongitude();
    Toast.makeText( getApplicationContext(),txt,Toast.LENGTH_SHORT).show();

    webview.loadUrl("http://www.myurl.com/page.php?this=" + txt);
}
暖伴 2025-01-13 09:00:07

此处添加新行:

public class WTest2Activity extends Activity {
    public String txt;
    public Location location;

然后,在您的 public void onLocationChanged(Location loc) 中,尝试设置 location.setLatitude(loc.getLatitide);location.setLongitude (loc.getLongitude);

然后,您可以使用全局变量 location 在任何地方访问您的位置

add a new line here:

public class WTest2Activity extends Activity {
    public String txt;
    public Location location;

then, in your public void onLocationChanged(Location loc), try setting location.setLatitude(loc.getLatitide); and location.setLongitude(loc.getLongitude);

then, you can access your location anywhere with your global variable location

昔梦 2025-01-13 09:00:07

兄弟,这只是你代码中的一个小逻辑错误。让我解释一下。您在加载 URL 后立即请求位置更新。现在位置监听是不同的线程,它正在更新 txt 变量的值。并且更新位置需要时间。而且每次花费的时间可能不同。这就是为什么您需要将 loadUrl 代码移至 onLocationChanged 方法。

Buddy its just small logical mistake in your code. Let me explain. You are requesting location updates then right after that you are loading URL. Now location listening is different thread which is updating value of txt variable. And updating location takes time. And taken time can be different every time. That is why you need to move your loadUrl code to onLocationChanged method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文