1

我有一个监控加速度计和 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 无效。

Java 代码

谢谢你,新年快乐。

代码

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();
}

}

4

3 回答 3

1

尝试先启动谷歌地图,让它精确定位您的当前位置。有时 Location 返回 null 因为最近的位置列表已被清除。还可以尝试这种方法来迭代当前启用的提供程序,而不仅仅是最好的提供程序。这使您有更高的机会或返回一些坐标。希望这对人有所帮助。祝你好运

private Location getLastKnownLocation() {
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
    Location l = mLocationManager.getLastKnownLocation(provider);
    ALog.d("last known location, provider: %s, location: %s", provider,
            l);

    if (l == null) {
        continue;
    }
    if (bestLocation == null
            || l.getAccuracy() < bestLocation.getAccuracy()) {
        ALog.d("found best last known location: %s", l);
        bestLocation = l;
    }
}
if (bestLocation == null) {
    return null;
}
return bestLocation;

}

于 2013-01-04T00:05:50.260 回答
1

你可以试试下面

locationText.setText(location);

代替

locationText = TextView.class.cast(location);

我希望 locationText 是一个 TextView 和

您正确请求了位置更新,并且

在清单中正确添加了权限,并且

位置更新代码没有被非 ui 线程执行。

于 2013-01-01T15:21:08.950 回答
1

您可能关闭了位置服务,请检查该提供商是否首先可用;

locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)

就像@Javantor 所说,您需要确保清单包括;

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

并且您正在更新 UI 线程。尝试将其放入 onLocationUpdated 代码中;

       runOnUiThread(new Runnable(){

            @Override
            public void run() {
                locationText.setText(location);
            }

        });

虽然它不应该有所作为,但以这种方式请求 LocationManager;

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

将时间设置为 0 而不是 1000 毫秒可确保您尽快获得更新。

您可以在 onLocationChanged 方法中添加一些日志记录吗?这将让您知道是否正在接收 GPS 数据。

于 2013-01-03T14:54:31.480 回答