我有一个应用程序可以监控加速度计和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则没有。
感谢你,祝你新年快乐。
CODE
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();
}
}
答案 0 :(得分:1)
你可以尝试下面的
locationText.setText(location);
而不是
locationText = TextView.class.cast(location);
我希望locationText是TextView和
您正确请求了位置更新
正确添加了清单中的权限并
非ui线程没有执行位置更新代码。
答案 1 :(得分: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数据。
答案 2 :(得分:1)
首先尝试启动Google地图,然后让它精确定位您当前的位置。有时,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;
}