Download presentation
Presentation is loading. Please wait.
Published byMelina Perkins Modified over 9 years ago
1
Getting Started with Android APIs Ivan Wong
2
Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are available
3
Integrating Barcode Scanner http://code.tutsplus.com/tutorials/android-sdk-create-a- barcode-reader--mobile-17162
4
Step 1: Create new App
5
Create Text Fields and Two Buttons <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"http://schemas.android.com/apk/res/android xmlns:tools="http://schemas.android.com/tools"http://schemas.android.com/tools android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/scan_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="@string/scan" /> <TextView android:id="@+id/scan_format" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:layout_centerHorizontal="true" android:layout_below="@id/scan_button" /> <TextView android:id="@+id/scan_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textIsSelectable="true" android:layout_centerHorizontal="true" android:layout_below="@id/scan_format" /> Scan
6
Add Zebra Crossing (ZXING) from GitHub https://code.google.com/p/zxing/source/browse/trunk/an droid- integration/src/com/google/zxing/integration/android/ ?r=2002
7
@Override for Oncreate private Button scanBtn; private TextView formatTxt, contentTxt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scanBtn = (Button)findViewById(R.id.scan_button); formatTxt = (TextView)findViewById(R.id.scan_format); contentTxt = (TextView)findViewById(R.id.scan_content); scanBtn.setOnClickListener(this); } public void onClick(View v){ //respond to clicks if(v.getId()==R.id.scan_button){ //scan IntentIntegrator scanIntegrator = new IntentIntegrator(this); scanIntegrator.initiateScan(); } public void onActivityResult(int requestCode, int resultCode, Intent intent) { //retrieve scan result IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if (scanningResult != null) { //we have a result String scanContent = scanningResult.getContents(); String scanFormat = scanningResult.getFormatName(); formatTxt.setText("FORMAT: " + scanFormat); contentTxt.setText("CONTENT: " + scanContent); } else{ Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT); toast.show(); }
8
What can this be used for? Integrating barcode scanning into your own application using ZXING library. Learning how to use open-source APIs which can help you in your app-development career.
9
Quick look at using built in Accels http://androidexample.com/Accelerometer_Basic_Example_- _Detect_Phone_Shake_Motion/index.php?view=article_discription& aid=109&aaid=131 - Detecting phone movements (jerk) <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" tools:context=".MainAccelerometer" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Shake / Tilt Your Phone To Get Accelerometer Motion Alerts" />
10
MainAccel.java public void onShake(float force) { // Do your stuff here // Called when Motion Detected Toast.makeText(getBaseContext(), "Motion detected", Toast.LENGTH_SHORT).show(); } @Override public void onResume() { super.onResume(); Toast.makeText(getBaseContext(), "onResume Accelerometer Started", Toast.LENGTH_SHORT).show(); //Check device supported Accelerometer senssor or not if (AccelerometerManager.isSupported(this)) { //Start Accelerometer Listening AccelerometerManager.startListening(this); }} @Override public void onStop() { super.onStop(); //Check device supported Accelerometer senssor or not if (AccelerometerManager.isListening()) { //Start Accelerometer Listening AccelerometerManager.stopListening(); Toast.makeText(getBaseContext(), "onStop Accelerometer Stoped", Toast.LENGTH_SHORT).show(); }} http://protolab.pbworks.com/f/1172890711/accel-01.jpg
11
Listener public interface AccelerometerListener { public void onAccelerationChanged(float x, float y, float z); public void onShake(float force); }
12
private static SensorEventListener sensorEventListener = new SensorEventListener() { public void onAccuracyChanged(Sensor sensor, int accuracy) {} public void onSensorChanged(SensorEvent event) { now = event.timestamp; x = event.values[0]; y = event.values[1]; z = event.values[2]; // if not interesting in shake events // just remove the whole if then else block if (lastUpdate == 0) { lastUpdate = now; lastShake = now; lastX = x; lastY = y; lastZ = z; Toast.makeText(aContext,"No Motion detected", Toast.LENGTH_SHORT).show(); } else { timeDiff = now - lastUpdate; if (timeDiff > 0) { /*force = Math.abs(x + y + z - lastX - lastY - lastZ) / timeDiff;*/ force = Math.abs(x + y + z - lastX - lastY - lastZ); if (Float.compare(force, threshold) >0 ) { //Toast.makeText(Accelerometer.getContext(), //(now-lastShake)+" >= "+interval, 1000).show(); if (now - lastShake >= interval) { // trigger shake event }; listener.onShake(force); } else { Toast.makeText(aContext,"No Motion detected", Toast.LENGTH_SHORT).show(); } lastShake = now; } lastX = x; lastY = y; lastZ = z; lastUpdate = now; } else { Toast.makeText(aContext,"No Motion detected", Toast.LENGTH_SHORT).show(); } // trigger change event listener.onAccelerationChanged(x, y, z); }
13
Applications? For you to be creative!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.