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

位置情報サービスがオフになっている可能性があります。プロバイダーが最初に利用可能であることを確認してください。

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

時間を 1000 ミリ秒ではなく 0 に設定すると、更新を可能な限り高速に取得できます。

onLocationChanged メソッドにログを追加できますか? これにより、GPS データがまったく受信されているかどうかがわかります。

于 2013-01-03T14:54:31.480 に答える
1

以下で試してみませんか

locationText.setText(location);

それ以外の

locationText = TextView.class.cast(location);

私は locationText が TextView であることを期待しています

位置情報の更新を正しくリクエストし、

マニフェストに権限を正しく追加し、

場所の更新コードは非 UI スレッドによって実行されません。

于 2013-01-01T15:21:08.950 に答える
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;

}

于 2013-01-04T00:05:50.260 に答える