首页 > 解决方案 > Android Things 将 GPS 数据发送到 TextView

问题描述

我在Raspberry Pi 3上使用Android Things v1.0.1 和Adafruit Ultimate GPS Breakout Board v3 。

我正在尝试在屏幕上显示GPS信息,特别是向显示屏上的个人发送LatLongAltitude和卫星数量,TextView但我很挣扎。

我正在使用的NMEA驱动程序是: Github / Android Things / Contrib Drivers / GPS

这是我的MainActivity

public class MainActivity extends Activity {

private static final String UART_DEVICE_NAME = "UART0";

UartDevice mDevice;
NmeaGpsModule mGpsModule;
NmeaParser mGpsModuleCallback;
NmeaGpsDriver mGpsDriver;
LocationManager mLocMan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setTitle("GPS Test");

    try {
        NmeaGpsDriver mGpsDriver = new NmeaGpsDriver(
                MainActivity.this,
                "UART0",
                9600,
                1.8f );

        mGpsDriver.register();

    } catch (IOException e) {
        // couldn't configure the gps driver...
        Log.w(TAG, "Couldn't configure the GPS driver");
    }

    mLocMan = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "No permission");
        return;
    }

    mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            0, 0, mLocList);
    mLocMan.registerGnssStatusCallback(mStatusCallback);
    mLocMan.addNmeaListener(mMessageListener);

    Button gpsbtn = (Button) findViewById(R.id.gpsbtn);
    gpsbtn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            Log.i(TAG, "Updating Location...");
        }
    }
    );
}

    private LocationListener mLocList = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.i(TAG, "Latitude: " + location.getLatitude());
            Log.i(TAG, "Latitude: " + location.getLongitude());
            Log.i(TAG, "Altitute: " + location.getAltitude());

            TextView latbox = new TextView(MainActivity.this);
            latbox.findViewById(R.id.latbox);
            latbox.setText(location.getLatitude());

            TextView longbox = new TextView(MainActivity.this);
            longbox.findViewById(R.id.longbox);
            longbox.setText(location.getLongitude());

            TextView altbox = new TextView(MainActivity.this);
            altbox.findViewById(R.id.altbox);
            altbox.setText(location.getAltitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) { }

        @Override
        public void onProviderEnabled(String provider) { }

        @Override
        public void onProviderDisabled(String provider) { }

    };

    /** Report satellite status */
    private GnssStatus.Callback mStatusCallback = new GnssStatus.Callback() {
        @Override
        public void onStarted() {
            Log.i(TAG, "GNSS Callback Started");
        }

        @Override
        public void onStopped() {
            Log.i(TAG, "GNSS Callback Stopped");
        }

        @Override
        public void onFirstFix(int ttffMillis) {
            Log.i(TAG, "On First Fix???");
        }

        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {
            Log.i(TAG, "GNSS Status: " + status.getSatelliteCount() + " satellites.");
        }
    };

private OnNmeaMessageListener mMessageListener = new OnNmeaMessageListener() {
    @Override
    public void onNmeaMessage(String message, long timestamp) {
        Log.v(TAG, "NMEA: " + message);
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mDevice != null) {
        try {
            mDevice.close();
            mDevice = null;
            mGpsDriver.unregister();
            mGpsDriver.close();
        } catch (IOException e) {
            Log.w(TAG, "Unable to close" + UART_DEVICE_NAME, e); }
    }
}

activity_main.xml

<TextView
    android:id="@+id/longbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<Button
    android:id="@+id/gpsbtn"
    style="@android:style/Widget.Button"
    android:layout_width="267dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:text="@string/gpsbtn"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/altbox"
    app:layout_constraintVertical_bias="0.447" />

<TextView
    android:id="@+id/loctext"
    android:layout_width="66dp"
    android:layout_height="21dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="@string/loctext"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/citybox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/latbox" />

<TextView
    android:id="@+id/lattext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="22dp"
    android:text="@string/lattext"
    app:layout_constraintEnd_toStartOf="@+id/latbox"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/longtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="22dp"
    android:text="@string/longtext"
    app:layout_constraintEnd_toStartOf="@+id/longbox"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/citytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="28dp"
    android:text="@string/citytext"
    app:layout_constraintEnd_toStartOf="@+id/citybox"
    app:layout_constraintTop_toBottomOf="@+id/lattext" />

<TextView
    android:id="@+id/countrytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="28dp"
    android:text="@string/countrytext"
    app:layout_constraintEnd_toStartOf="@+id/countrybox"
    app:layout_constraintTop_toBottomOf="@+id/longtext" />

<TextView
    android:id="@+id/latbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/countrybox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/longbox" />

<TextView
    android:id="@+id/alttext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="30dp"
    android:text="@string/alttext"
    app:layout_constraintEnd_toStartOf="@+id/altbox"
    app:layout_constraintTop_toBottomOf="@+id/citytext" />

<TextView
    android:id="@+id/altbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/citybox" />

<TextView
    android:id="@+id/fixtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="30dp"
    android:text="@string/fixtext"
    app:layout_constraintEnd_toStartOf="@+id/fixbox"
    app:layout_constraintTop_toBottomOf="@+id/countrytext" />

<TextView
    android:id="@+id/fixbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/countrybox" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="302dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintGuide_percent="0.5"
    app:layout_constraintTop_toTopOf="parent" />

标签: gpstextviewraspberry-pi3android-thingsadafruitandroid

解决方案


而是在方法中使用以下内容onLocationChanged()

TextView latbox = (TextView) findViewById(R.id.latbox);
latbox.setText("" + location.getLatitude());

TextView longbox = (TextView) findViewById(R.id.longbox);
longbox.setText("" + location.getLongitude());

TextView altbox = (TextView) findViewById(R.id.altbox);
altbox.setText("" + location.getAltitude());

推荐阅读