消息未在仿真器屏幕上显示

时间:2012-04-17 18:02:54

标签: android android-layout android-emulator

以下是查找当前位置的代码,但在按下后退按钮后屏幕上没有显示任何内容。
我重置了adb并尝试手动从模拟器控件发送数据。但是当我使用模拟器控制发送数据时,模拟器重新启动但从不显示主屏幕 请帮忙。提前谢谢。

public class LbsGeocodingActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters

    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds

    protected LocationManager locationManager;

    protected Button retrieveLocationButton;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
                new MyLocationListener()

        );
    retrieveLocationButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                showCurrentLocation();
            }
    });       

    }   

    protected void showCurrentLocation() {
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
            String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );
            Toast.makeText(LbsGeocodingActivity.this, message,
                    Toast.LENGTH_LONG).show();
        }
    }  

    private class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude()
            );

            Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }

        public void onStatusChanged(String s, int i, Bundle b) {
            Toast.makeText(LbsGeocodingActivity.this, "Provider status changed",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderDisabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider disabled by the user. GPS turned off",
                    Toast.LENGTH_LONG).show();
        }

        public void onProviderEnabled(String s) {
            Toast.makeText(LbsGeocodingActivity.this,
                    "Provider enabled by the user. GPS turned on",
                    Toast.LENGTH_LONG).show();
        }

    }
}

2 个答案:

答案 0 :(得分:1)

在您的代码中,您可能没有收到当前位置(这是因为您需要通过emulator control手动推送您的位置。)

在showCurrentLocation()中还有其他情况,并检查位置对象是否为null,如下所示。

protected void showCurrentLocation() {
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );

    Toast.makeText(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }else{
       Toast.makeText(LbsGeocodingActivity.this, "location is null",
                Toast.LENGTH_LONG).show();
    }
}  

答案 1 :(得分:0)

在我们第一次尝试使用位置传感器时,我们遇到了类似的问题。为了从GPS中获得最佳效果,有两件事情最有帮助。

在模拟器上, Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 会导致很多力量关闭。

最重要的是。通过telnet输入gps数据要稳定得多。 telnet localhost 5554 您可以在模拟器窗口标题上看到端口)。新gps修复的命令是: geo fix lat lng