Last active
December 16, 2015 16:40
-
-
Save peplin/5464835 to your computer and use it in GitHub Desktop.
Complete example "Hello Car" app for OpenXC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity" > | |
<TextView | |
android:id="@+id/textView1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/hello_world" /> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/textView1" | |
android:id="@+id/vehicle_speed" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.test" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="8" | |
android:targetSdkVersion="17" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.example.test.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<service android:name="com.openxc.VehicleManager"/> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.test; | |
import android.app.Activity; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.os.Bundle; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.widget.TextView; | |
import com.openxc.VehicleManager; | |
import com.openxc.measurements.Measurement; | |
import com.openxc.measurements.UnrecognizedMeasurementTypeException; | |
import com.openxc.measurements.VehicleSpeed; | |
import com.openxc.remote.VehicleServiceException; | |
public class MainActivity extends Activity { | |
private VehicleManager mVehicleManager; | |
private TextView mVehicleSpeedView; | |
private ServiceConnection mConnection = new ServiceConnection() { | |
// Called when the connection with the service is established | |
public void onServiceConnected(ComponentName className, | |
IBinder service) { | |
Log.i("openxc", "Bound to VehicleManager"); | |
mVehicleManager = ((VehicleManager.VehicleBinder)service).getService(); | |
try { | |
mVehicleManager.addListener(VehicleSpeed.class, mSpeedListener); | |
} catch (VehicleServiceException e) { | |
e.printStackTrace(); | |
} catch (UnrecognizedMeasurementTypeException e) { | |
e.printStackTrace(); | |
} | |
} | |
// Called when the connection with the service disconnects unexpectedly | |
public void onServiceDisconnected(ComponentName className) { | |
Log.w("openxc", "VehicleService disconnected unexpectedly"); | |
mVehicleManager = null; | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mVehicleSpeedView = (TextView) findViewById(R.id.vehicle_speed); | |
Intent intent = new Intent(this, VehicleManager.class); | |
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
public void onPause() { | |
super.onPause(); | |
Log.i("openxc", "Unbinding from vehicle service"); | |
unbindService(mConnection); | |
} | |
VehicleSpeed.Listener mSpeedListener = new VehicleSpeed.Listener() { | |
public void receive(Measurement measurement) { | |
final VehicleSpeed speed = (VehicleSpeed) measurement; | |
MainActivity.this.runOnUiThread(new Runnable() { | |
public void run() { | |
mVehicleSpeedView.setText( | |
"Vehicle speed (km/h): " + speed.getValue().doubleValue()); | |
} | |
}); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment