Android:GPS 坐标可以在模拟器上工作,但不能在手机上工作

标签 android gps emulation

我是一名 Android 初学者,我需要你的帮助!我正在构建一个非常简单的运行应用程序,并且希望收集有关速度的信息。为此,我尝试创建一个非常简单的 GPS 类,以便使用 getspeed() 方法。

我创建了一个小测试应用程序来确保我的代码是正确的。在此应用程序中,我也使用 getLatitude() 和 getLongitude() 但这些对我的最终代码没有用 - 我真正需要的只是速度。

到目前为止,我已经成功地在模拟器上测试了该类,但未能在手机上测试。使用 DDMS 坐标时,我可以看到屏幕上填充的坐标(getspeed() 仍然为 0,但我知道我无法在模拟器上测试它)。当我在手机上尝试该代码时,我完全没有任何响应 - 尽管正在等待 GPS“预热”(当我移动到 GoogleMaps 时,我还发现 GPS 可以工作)。

整件事让我发疯,我不确定 onLocationChanged() 方法发生了什么,所以感谢您能给我的任何帮助。 谢谢!

代码如下:

    import android.app.Activity; 
    import android.content.Context; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.widget.TextView;
    import android.widget.Toast; 

    public class MainActivity extends Activity { 
    TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;

Context context;

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


    LocationListener locationListener = new LocationListener() { 
        public void onLocationChanged(Location location) { 

            Toast.makeText(getBaseContext(), "location not null" , Toast.LENGTH_SHORT).show();


            speedText1 = (TextView) findViewById(R.id.textView1);
            speedText2 = (TextView) findViewById(R.id.textView2);
            speedText3 = (TextView) findViewById(R.id.textView3);

            speed = location.getSpeed();
            speed1 = Double.toString(speed);
            speedText1.setText(speed1);

            latitude = location.getLatitude();
            latitude1 = Double.toString(latitude);
            speedText2.setText(latitude1);

            longitude = location.getLongitude();
            longitude1 = Double.toString(longitude);
            speedText3.setText(longitude1);

             Toast.makeText(getBaseContext(), latitude1 + longitude1 + speed , Toast.LENGTH_SHORT).show();



        }     

        public void onStatusChanged(String provider, int status, Bundle extras) { 

        } 

        public void onProviderEnabled(String provider) { 

        } 

        public void onProviderDisabled(String provider) {

        } }; 
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}

最佳答案

您在 onCreate() 方法中定义 LocationListener,因此该方法完成后您将不会获得位置更新。你应该尝试这样的事情:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView;
import android.widget.Toast; 

public class MainActivity extends Activity implements LocationListener { 

TextView speedText1, speedText2, speedText3;
double speed = 0;
double latitude = 0;
double longitude = 0;
String speed1;
String latitude1;
String longitude1;
boolean gps_enabled;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);


    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 0.0f, this);
    gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);


    Toast.makeText(this, "location not null" , Toast.LENGTH_SHORT).show();


    speedText1 = (TextView) findViewById(R.id.textView1);
    speedText2 = (TextView) findViewById(R.id.textView2);
    speedText3 = (TextView) findViewById(R.id.textView3);

    }

    @Override
    public void onLocationChanged(Location location) {

        speed = location.getSpeed();
        speed1 = Double.toString(speed);
        speedText1.setText(speed1);

        latitude = location.getLatitude();
        latitude1 = Double.toString(latitude);
        speedText2.setText(latitude1);

        longitude = location.getLongitude();
        longitude1 = Double.toString(longitude);
        speedText3.setText(longitude1);

        Toast.makeText(this, latitude1 + longitude1 + speed, Toast.LENGTH_SHORT).show();            

    }

}

}

关于Android:GPS 坐标可以在模拟器上工作,但不能在手机上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21205651/

相关文章:

安卓异步调用

android - GpsSatellite.getSnr() - 取值范围是多少?

Android 模拟 gps 供应商

time - Android:日历不给出全年数字,只给出 111 而不是 2011

android - 如何让 SwipeRefreshLayout 在 ActionBar 中设置文本

android - 需要帮助改进 OpenCV 2.4.0 中人脸检测的执行时间

emulation - 模拟CPU时关于循环计数准确性的问题

android - 如何从代码中找出我的 Android 应用程序是在模拟器还是真实设备上运行?

android - 如何在 Android 的 AccountManager 中删除帐户

mysql - 如何防止错误 Mysql ST_PolygonFromText 因为第一个和最后一个坐标不等于