吐司循环中无法正常显示 [英] Toast in loop is not displaying properly

查看:200
本文介绍了吐司循环中无法正常显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的主要活动如下code(注: GPSTracker 在此应用程序的作品):

 双纬度,经度;
    GPS =新GPSTracker(MainActivity.this);
    如果(gps.canGetLocation()){
         纬度= gps.getLatitude();
         经度= gps.getLongitude();
         Toast.makeText(getApplicationContext(),您现在的位置是 - \\ nLat:+纬度+\\ nLong:+经度,Toast.LENGTH_LONG).show();
    }
    其他{
         gps.showSettingsAlert();
    }

我要创建一个循环,这将在一定的时间间隔吐司我目前的职位显示。从来就试过这样:

 双纬度,经度;
    长currentTime的= System.currentTimeMillis的();
    长myTimestamp = currentTime的;
    INT I = 0;
    GPS =新GPSTracker(MainActivity.this);
    而(ⅰ小于5)
    {
        myTimestamp = System.currentTimeMillis的();
        如果((myTimestamp - currentTime的)GT; 5000)
        {
            我++;
            currentTime的= System.currentTimeMillis的();
            如果(gps.canGetLocation()){
                纬度= gps.getLatitude();
                经度= gps.getLongitude();
                Toast.makeText(getApplicationContext(),您现在的位置是 - \\ nLat:+纬度+\\ nLong:+经度,Toast.LENGTH_LONG).show();
            }其他{
                gps.showSettingsAlert();
            }
        }
    }

有了这个code,吐司只显示一次(最后迭代)。你能不能帮我这个?先谢谢了。


解决方案

  

我希望它显示每个迭代(例如每5秒)。


上面的code不会每五秒钟循环,它循环不断,但只有你递增计数器每五秒钟......这是创建时间延迟的非常低效的方式,因为没有别的可以同时循环恰巧运行。 (即使你在一个单独的线程上运行这一点,仍然没有很好的战术。)

而是使用的LocationManager的<一个href=\"https://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29\"相对=nofollow> requestLocationUpdates 将使用回调让你的应用程序可以更新之间做的事情。一对夫妇的快速说明:


  • 记者了解到,GPS可能无法得到修复每五秒钟,这个时间间隔非常短所以应谨慎使用,否则你会耗尽电量下降。

  • 某些pre-果冻豆设备可能不遵守 minTime 参数,但你可以自己执行自己的时间参数作为我的 Android的定位监听通话非常频繁


抛开一切,你使用现有的code,但我推荐一个处理程序和运行的,就像这样:

  handler.postDelayed(新的Runnable(){
    @覆盖
    公共无效的run(){
        //获取您的位置在这里        //约5秒钟再次运行code
        handler.postDelayed(这一点,5000);
    }
},5000);

I have the following code in my main activity (Note: GPSTracker in this application works):

    double latitude, longitude;
    gps = new GPSTracker(MainActivity.this);
    if(gps.canGetLocation()){
         latitude = gps.getLatitude();
         longitude = gps.getLongitude();
         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
    }
    else{
         gps.showSettingsAlert();
    }

I want to create a loop, which would display in some time intervals Toast with my current position. I´ve tried this:

    double latitude, longitude;
    long currentTime = System.currentTimeMillis();
    long myTimestamp = currentTime;
    int i = 0;
    gps = new GPSTracker(MainActivity.this);
    while(i < 5)
    {
        myTimestamp = System.currentTimeMillis();
        if((myTimestamp - currentTime) > 5000)
        {
            i++;
            currentTime = System.currentTimeMillis();
            if(gps.canGetLocation()){
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();  
            }else{
                gps.showSettingsAlert();
            }
        }
    }

With this code, Toast is shown only one time (the last iteration). Could you help me with this? Thanks in advance.

解决方案

I want it to be shown every iteration (for example every 5 seconds).

The code above doesn't loop every five seconds, it loops continuously but only increments your counter every five seconds... This is a very inefficient way of creating a time delay because nothing else can happen while the loop runs. (Even if you run this on a separate thread it is still not good tactic.)

Instead use LocationManager's requestLocationUpdates which will use callbacks so your app can do things between updates. A couple quick notes:

  • Understand that the GPS may not be able to get a fix every five seconds and that this interval is really short so use it sparingly or you'll run the battery down.
  • Some pre-Jelly Bean devices may not observe the minTime parameter, but you can enforce your time parameter yourself as I describe in Android Location Listener call very often.

All that aside, you use your existing code but I recommend a Handler and Runnable, like this:

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Fetch your location here

        // Run the code again in about 5 seconds
        handler.postDelayed(this, 5000);
    }
}, 5000);

这篇关于吐司循环中无法正常显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆