Android:GPS坐标可在仿真器上运行,但不能在手机上运行

时间:2014-01-18 14:49:55

标签: android gps emulation

我是一名机器人初学者,我需要你的帮助!我正在构建一个非常简单的运行应用程序,并希望收集有关速度的信息。为此,我试图创建一个非常简单的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);
    }
}

1 个答案:

答案 0 :(得分:2)

您正在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();            

    }

}

}