我有一个监控加速度计和 GPS 并将它们输出到文件中的应用程序。
我已默认 GPS 为 0,0。但它似乎没有改变。
public String location = "0 , 0";
public void onLocationChanged(Location arg0) {
arg0.getLatitude();
arg0.getLongitude();
location = arg0.getLongitude() + "," + arg0.getLatitude();
locationText = TextView.class.cast(location);
}
文本视图可以显示它是否正在发生变化,但这似乎也没有更新。
位置文本视图位于屏幕顶部。
我不确定我还能包括什么来帮助您帮助我。但如果有什么让我知道。
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
这是加速度计的代码,它有效,而 GPS 无效。
谢谢你,新年快乐。
代码
public class AccelOrientExample extends Activity implements SensorEventListener, LocationListener {
// Accelerometer X, Y, and Z values
private TextView accelXValue;
private TextView accelYValue;
private TextView accelZValue;
// Orientation X, Y, and Z values
private TextView orientXValue;
private TextView orientYValue;
private TextView orientZValue;
private TextView locationText;
private TextView toWrite;
public Toast myToast;
public String accel;
public int counter = 0;
public String orientData;
private LocationManager lm;
private Toast loc;
public String location = "0 , 0";
private SensorManager sensorManager = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a reference to a SensorManager
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
// Capture accelerometer related view elements
accelXValue = (TextView) findViewById(R.id.accel_x_value);
accelYValue = (TextView) findViewById(R.id.accel_y_value);
accelZValue = (TextView) findViewById(R.id.accel_z_value);
// Capture orientation related view elements
orientXValue = (TextView) findViewById(R.id.orient_x_value);
orientYValue = (TextView) findViewById(R.id.orient_y_value);
orientZValue = (TextView) findViewById(R.id.orient_z_value);
// Initialize accelerometer related view elements
accelXValue.setText("0.00");
accelYValue.setText("0.00");
accelZValue.setText("0.00");
// Initialize orientation related view elements
orientXValue.setText("0.00");
orientYValue.setText("0.00");
orientZValue.setText("0.00");
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
/**/
// This method will update the UI on new sensor events
public void onSensorChanged(SensorEvent sensorEvent) {
synchronized (this) {
while(counter < 15)
{
if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
orientXValue.setText(Float.toString(sensorEvent.values[0]));
orientYValue.setText(Float.toString(sensorEvent.values[1]));
orientZValue.setText(Float.toString(sensorEvent.values[2]));
}
MyFile file = new MyFile();
file.createFile("OrientData.txt");
orientData = sensorEvent.values[0] + "," + sensorEvent.values[1] + "," + sensorEvent.values[2] + "\n";
file.write(orientData);
counter ++;
}
if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
accelXValue.setText(Float.toString(sensorEvent.values[0]));
accelYValue.setText(Float.toString(sensorEvent.values[1]));
accelZValue.setText(Float.toString(sensorEvent.values[2]));
accel = sensorEvent.values[0] + "," + sensorEvent.values[1] + "," + sensorEvent.values[2] + ",";
}
}
}
public void onLocationChanged(Location arg0) {
synchronized (this) {
lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
arg0.getLatitude();
arg0.getLongitude();
location = arg0.getLongitude() + "," + arg0.getLatitude();
Toast.makeText(getApplicationContext(),"Test",Toast.LENGTH_LONG).show();
runOnUiThread(new Runnable(){
public void run() {
locationText.setText(location);
}
});
}
}
public void FileLogger(String accel, String location)
{
MyFile file = new MyFile();
file.createFile("RoadData.txt");
String toWrite = accel + location + "\n";
file.write(toWrite);
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onProviderDisabled(String arg0) {
Log.e("GPS", "provider disabled " + arg0);
}
public void onProviderEnabled(String arg0) {
Log.e("GPS", "provider enabled " + arg0);
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]");
}
@Override
protected void onResume() {
super.onResume();
// Register this class as a listener for the accelerometer sensor
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
// ...and the orientation sensor
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
// Unregister the listener
sensorManager.unregisterListener(this);
super.onStop();
}
}