Mobile Software Development for Android - I397

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 4 Overview Overview of Sensors Programming Tutorial 1: Tracking location with GPS and Google.
Advertisements

- Rohit Vobbilisetty. What is the Camera API Capture Photos Capture Videos Camera API - Android2.
Multimedia.  Android provides built-in encoding/decoding for a variety of common media types  Allows you to play & record audio, still images & video.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Sensors.  Hardware devices that take measurements of the physical environment  Some examples  3-axis Accelerometer  3-axis Magnetic field sensor 
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
CS378 - Mobile Computing Sensing and Sensors. Sensors "I should have paid more attention in Physics 41" Most devices have built in sensors to measure.
Android Sensors & Async Callbacks Jules White Bradley Dept. of Electrical and Computer Engineering Virginia Tech
Android – Camera –an Example
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Getting Started with Android APIs Ivan Wong. Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are.
Mobile Application Development Selected Topics – CPIT 490
Lecture # 9 Hardware Sensor. Topics 2  SensorManger & Sensor  SensorEvent & SensorEventListener  Example Application.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
1 Localization and Sensing Nilanjan Banerjee Mobile Systems Programming (Acknowledgement: Jules White) University of Arkansas Fayetteville, AR
Sensing. Possible sensors on devices – Documented in SensorEvent class Accelerometer (m/s 2 ) – acceleration in x, y, z axes Magnetic Field (micro Tesla)
Doodlz App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Android Accessing GPS Ken Nguyen Clayton State University 2012.
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
Sensors – Part I SE 395/595.
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
Location. GPS Global Positioning System – At least 4 satellites typically used 3 required extra for error detection and altitude typically accurate within.
Maps Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Mobile Device Development Camera and Sensors Dr.YingLiang Ma.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.
Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using.
Augmented Reality Engine Viewlity. About me CristianAndreica 4 th Year student – Politehnica University of Bucharest Co-founder of XTWIP.com Currently.
Sensors Nasrullah Khan Niazi. Using Device Sensors The Android SDK provides access to raw data from sensors on the device.The sensors,and their precision.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors Date: Feb 11, 2016.
CPE 490/590 – Smartphone Development
Android Fundamentals. What is Android Software stack for mobile devices Software stack for mobile devices SDK provides tools and APIs to develop apps.
CHAPTER 8 Sensors and Camera. Chapter objectives: Understand Motion Sensors, Environmental Sensors and Positional Sensors Learn how to acquire measurement.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
CS371m - Mobile Computing Sensing and Sensors.
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
1. 2 Android location services Determining a device’s current location Tracking device movements Proximity alerts.
CS499 – Mobile Application Development Fall 2013 Location & Maps.
Android Android Sensors Android Sensors: – Accelerometer – Gravity sensor – Linear Acceleration sensor – Magnetic Field sensor – Orientation.
Sensors in Android.
Vijay Kumar Kolagani Dr. Yingcai Xiao
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors
TUTORIAL ON MULTITOUCH AND SWIPE GESTURES
Lecture 2: Android Concepts
Lecture 5: Location Topics: Google Play Services, Location API.
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
Sensors, maps and fragments:
Vijay Kumar Kolagani Dr. Yingcai Xiao
CS499 – Mobile Application Development
What can Android Sense for you?
CS371m - Mobile Computing Sensing and Sensors.
Recap: Android Components
CIS 470 Mobile App Development
Vijay Kumar Kolagani Dr. Yingcai Xiao
CIS 470 Mobile App Development
Cannon Game App Android How to Program
Activities and Intents
Activities and Intents
Android Programming Tutorial
CIS 493/EEC 492 Android Sensor Programming
Lecture 4: Sensors Topics: Motion, Position, and Environmental Sensors.
CIS 493/EEC 492 Android Sensor Programming
Lecture 5: Location Topics: Google Play Services, Location API.
Mobile Programming Sensors in Android.
Mobile Programming Gestures in Android.
CS378 - Mobile Computing Location and Maps.
SE4S701 Mobile Application Development
Mobile Programming Broadcast Receivers.
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Mobile Software Development for Android - I397 IT College, Andres Käver, 2015-2016 Email: akaver@itcollege.ee Web: http://enos.itcollege.ee/~akaver/2015-2016/Distance/Android Skype: akaver

Android Location Sensors Camera Touch Gestures Notifications GPS, Network, Passive Sensors Gyroscope, Compass, Orientation, Acceleration Camera Intent, Camera API Touch Single, Multi Gestures Notifications

Android – Location Most Android devices allow some form of geolocation Via WiFi Via cell-tower triangulation Via GPS LocationManager class Location providers Register location update listeners Proximity alerts ….

Android – Location LocationProvider Device might have several Network - Uses the mobile network or WI-Fi to determine the best location. Might have a higher precision in closed rooms then GPS. Gps - Use the GPS receiver in the Android device to determine the best location via satellites. Usually better precision than network. Passive - Allows to participate in location of updates of other components to save energy Use Criteria object for flexible selection Register LocationListener with LocationManager, to get periodic updates about geoposition

Android – Proximity alert Register an Intent Longitude, Latitude and radius Alert will be triggered, when device enters the predefined area

Android - GeoCoding Geocoder class Get geo-coordinates for given address Get possible address for given geolocation Uses online Google service

Android – Location - Security For GPS ACCESS_FINE_LOCATION Others ACCESS_COARSE_LOCATION

Android – GPS disabled GPS can be disabled by user Not possible to activate via software Find out via LocationManager – isProviderEnabled() method Send the user to the settings via an Intent with the Settings.ACTION_LOCATION_SOURCE_SETTINGS public void testGps(){ LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); boolean enabled = service .isProviderEnabled(LocationManager.GPS_PROVIDER); // check if enabled and if not send user to the GSP settings // Better solution would be to display a dialog and suggesting to // go to the settings if (!enabled) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } }

Android - GPS Very battery consuming Disable GPS updates, when not needed (ie in background) onResume() onPause()

Android - GPS getLastKnownLocation Gives back immediate result public class MainActivity extends AppCompatActivity implements LocationListener { getLastKnownLocation Gives back immediate result Can be null protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); latituteField = (TextView) findViewById(R.id.TextView02); longitudeField = (TextView) findViewById(R.id.TextView04); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); provider = locationManager.getBestProvider(criteria, false); Location location = locationManager.getLastKnownLocation(provider); if (location != null) { System.out.println("Provider " + provider + " has been selected."); onLocationChanged(location); } else { latituteField.setText("Location not available"); longitudeField.setText("Location not available"); } }

Android - GPS requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) minTime - minimum time interval between location updates, in milliseconds minDistance - minimum distance between location updates, in meters Listener - a LocationListener whose onLocationChanged(Location) method will be called for each location update /* Request updates at startup */ @Override protected void onResume() { super.onResume(); if (locationManager != null) { locationManager.requestLocationUpdates(provider, 400, 1, this); } } /* Remove the locationlistener updates when Activity is paused */ @Override protected void onPause() { super.onPause(); if (locationManager != null) { locationManager.removeUpdates(this); } }

Android - GPS Use onLocationChanged to receive position updates Mostly requires device for any sensible testing Much better in MS emulator or in Android Studio 2 DEMO @Override public void onLocationChanged(Location location) { double lat = location.getLatitude(); double lng = location.getLongitude(); latituteField.setText(String.valueOf(lat)); longitudeField.setText(String.valueOf(lng)); }

Android - Sensors Most android systems have sensors for Motion Accelerometer, Gyroscope, Gravity, Rotational vector Position Orientation, Magnetometer Environment Temperature, Pressure, Humidity, Illumination Sensor framework Determine availability Sensor capabilities – range, resolution, manufacturer, power usage Acquire raw data, register listeners

Android - Sensors TYPE_ACCELEROMETER TYPE_AMBIENT_TEMPERATURE TYPE_GRAVITY TYPE_GYROSCOPE TYPE_LIGHT TYPE_LINEAR_ACCELERATION TYPE_MAGNETIC_FIELD TYPE_ORIENTATION TYPE_PRESSURE TYPE_PROXIMITY TYPE_RELATIVE_HUMIDITY TYPE_ROTATION_VECTOR TYPE_TEMPERATURE

Android - Sensors Sensor Framework SensorManager Sensor SensorEvent Access and list sensors, register listeners, sensor accuracy, calibration, data rates Sensor Instance of specific sensor. Methods for determining sensor capabilities. SensorEvent Raw sensor data, type of sensor, accuracy, timestamp SensorEventListener Callback interface, receive sensor events

Android - Sensors Sesnor availability 2 – sensor is deprecated

Android - Sensors Identifying Sensors Get listing of sensors Get reference to sensor service Get listing of sensors getSensorList(Sensor.TYPE_ALL) TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION, or TYPE_GRAVITY Determine, if there is sensor of certain type getDefaultSensor() There can be several sensors of same type one is designated as default private SensorManager mSensorManager; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); List<Sensor> deviceSensors = mSensorManager.getSensorList(Sensor.TYPE_ALL); private SensorManager mSensorManager; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); if (mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){   // Success! There's a magnetometer.   } else {   // Failure! No magnetometer.   }

Android - Sensors There are no requirements from Android, that certain sensors are provided by manufacturers! Determine the capabilities and attributes of individual sensors Sensor class getResolution() getMaximumRange() getPower() getMinDelay() – microseconds 0 – only events, >0 - streaming getVendor() getVersion() private SensorManager mSensorManager; private Sensor mSensor; ... mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); if (mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){   List<Sensor> gravSensors = mSensorManager.getSensorList(Sensor.TYPE_GRAVITY);   for(int i=0; i<gravSensors.size(); i++) {     if ((gravSensors.get(i).getVendor().contains("Google Inc.")) &&        (gravSensors.get(i).getVersion() == 3)){       // Use the version 3 gravity sensor.       mSensor = gravSensors.get(i);     }   } } else{   // Use the accelerometer.   if (mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){     mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);   }   else{     // Sorry, there are no accelerometers on your device.     // You can't play this game.   } }

Android - Sensors Monitoring Sensor Events - SensorEventListener interface onAccuracyChanged() SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_MEDIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE. onSensorChanged() sensorEvent object contains info about new data Accuracy Sensor Timestap Sensor data reading

Android - Sensors Sensor report rates SENSOR_DELAY_NORMAL public class SensorActivity extends Activity implements SensorEventListener {   private SensorManager mSensorManager;   private Sensor mLight;   @Override   public final void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);     mLight = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);   }   @Override   public final void onAccuracyChanged(Sensor sensor, int accuracy) {   }   @Override   public final void onSensorChanged(SensorEvent event) {     // The light sensor returns a single value.     // Many sensors return 3 values, one for each axis.     float lux = event.values[0];     // Do something with this sensor value.   }   @Override   protected void onResume() {     super.onResume();     mSensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);   }   @Override   protected void onPause() {     super.onPause();     mSensorManager.unregisterListener(this);   } } Sensor report rates SENSOR_DELAY_NORMAL 200,000 microseconds SENSOR_DELAY_UI 60,000 microsecond delay SENSOR_DELAY_GAME 20,000 microsecond delay SENSOR_DELAY_FASTEST 0 microsecond delay) Or specific delay (api level 11)

Android - Sensors Detecting sensors at runtime   private SensorManager mSensorManager;   ...   mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);   if (mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) != null){   // Success! There's a pressure sensor.   }   else {   // Failure! No pressure sensor.   } Detecting sensors at runtime Your app might not need all the sensors Using Google Play filters to target specific sensor configurations When sensor is mandatory <uses-feature android:name="android.hardware.sensor.accelerometer"               android:required="true" />

Android - Sensors Coordinate system, based on default position Tablets are often defaulted to landscape Acceleration sensor Gravity sensor Gyroscope Linear acceleration sensor Geomagnetic field sensor

Android - Sensors Best practices You should choose the slowest sampling rate that still meets the needs of your application - System usually provides faster refresh rates Use onResume and onPause Be aware of power usage Sensors are not turned off, when screen turns off Detect sensors at runtime and enable or disable application features as appropriate. Use Google Play filters to target devices with specific sensor configurations. Don't block the onSensorChanged() method Verify sensors before you use them (and calibrate) DON’T TEST YOUR CODE ON EMULATOR

Android - Camera Most devices have at least one camera Most newer devices have 2 – back and front Either use existing camera app Intent Or use API android.hardware.camera2 API

Android – Camera - intent Intent based camera usage MediaStore.ACTION_IMAGE_CAPTURE MediaStore.EXTRA_OUTPUT with Uri MediaStore.ACTION_VIDEO_CAPTURE startActivityForResult() onActivityResult() <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android – Camera - Intent private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100; private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {         if (resultCode == RESULT_OK) {             // Image captured and saved to fileUri specified in the Intent             Toast.makeText(this, "Image saved to:\n" +                      data.getData(), Toast.LENGTH_LONG).show();         } else if (resultCode == RESULT_CANCELED) {             // User cancelled the image capture         } else {             // Image capture failed, advise user         }     }     if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {         if (resultCode == RESULT_OK) {             // Video captured and saved to fileUri specified in the Intent             Toast.makeText(this, "Video saved to:\n" +                      data.getData(), Toast.LENGTH_LONG).show();         } else if (resultCode == RESULT_CANCELED) {             // User cancelled the video capture         } else {             // Video capture failed, advise user         }     } } Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show(); // recycle unused bitmaps if (bitmap != null) { bitmap.recycle(); } stream = getContentResolver().openInputStream(data.getData()); bitmap = BitmapFactory.decodeStream(stream); imageView.setImageBitmap(bitmap);

Android - Camera - API When you want to do something specific or new Build your own Camera App Robotics etc

Android – Camera - API Detect and Access Camera Create a Preview Class - extend SurfaceView and implement the SurfaceHolder interface Build a Preview Layout - create a view layout that incorporates the preview and the user interface controls Setup Listeners for Capture - Connect listeners for your interface controls to start image or video capture in response to user actions Capture and Save Files Release the Camera

Android – Camera - API Require camera in manifest Or Detect camera <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required=“true" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> Or Detect camera /** Check if this device has a camera */ private boolean checkCameraHardware(Context context) {     if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){         // this device has a camera         return true;     } else {         // no camera on this device         return false;     } }

Android – Camera - API Camera.Open(int) Check exceptions! /** A safe way to get an instance of the Camera object. */ public static Camera getCameraInstance(){     Camera c = null;     try {         c = Camera.open(); // attempt to get a Camera instance     }     catch (Exception e){         // Camera is not available (in use or does not exist)     }     return c; // returns null if camera is unavailable } Camera.Open(int) Check exceptions! Camera.getParameters() Camera.getCameraInfo()

Android – Camera - API SurfaceView public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {     private SurfaceHolder mHolder;     private Camera mCamera;     public CameraPreview(Context context, Camera camera) {         super(context);         mCamera = camera;         mHolder = getHolder();         mHolder.addCallback(this);         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     }     public void surfaceCreated(SurfaceHolder holder) {         try {             mCamera.setPreviewDisplay(holder);             mCamera.startPreview();         } catch (IOException e) {Log.d(TAG, "Error setting camera preview: " + e.getMessage());}     }     public void surfaceDestroyed(SurfaceHolder holder) {}     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {         if (mHolder.getSurface() == null){return;}         try {mCamera.stopPreview();} catch (Exception e){}         try {             mCamera.setPreviewDisplay(mHolder);             mCamera.startPreview();         } catch (Exception e){Log.d(TAG, "Error starting camera preview: " + e.getMessage());}     } } SurfaceView

Android – Camera - API Place preview into layout   <FrameLayout     android:id="@+id/camera_preview"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:layout_weight="1"     /> public class CameraActivity extends Activity {     private Camera mCamera;     private CameraPreview mPreview;     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         // Create an instance of Camera         mCamera = getCameraInstance();         // Create our Preview view and set it as the content of our activity.         mPreview = new CameraPreview(this, mCamera);         FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);         preview.addView(mPreview);     } }

Android – Camera - API Take picture Camera.takePicture() private PictureCallback mPicture = new PictureCallback() {     @Override     public void onPictureTaken(byte[] data, Camera camera) {         File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);         if (pictureFile == null){             Log.d(TAG, "Error creating media file, check storage permissions: " +                 e.getMessage());             return;         }         try {             FileOutputStream fos = new FileOutputStream(pictureFile);             fos.write(data);             fos.close();         } catch (FileNotFoundException e) {             Log.d(TAG, "File not found: " + e.getMessage());         } catch (IOException e) {             Log.d(TAG, "Error accessing file: " + e.getMessage());         }     } }; Take picture Camera.takePicture() Camera.PictureCallback Remember to release the camera! // Add a listener to the Capture button Button captureButton = (Button) findViewById(id.button_capture); captureButton.setOnClickListener(     new View.OnClickListener() {         @Override         public void onClick(View v) {             // get an image from the camera             mCamera.takePicture(null, null, mPicture);         }     } );

Android – Camera - API Releasing camera     private Camera mCamera;     private SurfaceView mPreview;     private MediaRecorder mMediaRecorder;     ...     @Override     protected void onPause() {         super.onPause();         releaseMediaRecorder();       // if you are using MediaRecorder, release it first         releaseCamera();              // release the camera immediately on pause event     }     private void releaseMediaRecorder(){         if (mMediaRecorder != null) {             mMediaRecorder.reset();   // clear recorder configuration             mMediaRecorder.release(); // release the recorder object             mMediaRecorder = null;             mCamera.lock();           // lock camera for later use         }     }     private void releaseCamera(){         if (mCamera != null){             mCamera.release();        // release the camera for other applications             mCamera = null;         }     }

Android – Camera - API Camera features Most can be set using Camera.Parameters But not all Metering and Focus areas Face detection Time lapse video

Android – Camera - API GPS Data White Balance Focus Mode Scene Mode Face Detection Metering Areas Focus Areas White Balance Lock Exposure Lock Video Snapshot Time Lapse Video Multiple Cameras Focus Distance Zoom Exposure Compensation GPS Data White Balance Focus Mode Scene Mode JPEG Quality Flash Mode Color Effects Anti-Banding Picture Format Picture Size

Android – Camera - API Checking feature availability Camera.Parameters object provides a getSupported...(), is...Supported() or getMax...() method to determine if (and to what extent) a feature is supported. // get Camera parameters Camera.Parameters params = mCamera.getParameters(); List<String> focusModes = params.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {   // Autofocus mode is supported } // get Camera parameters Camera.Parameters params = mCamera.getParameters(); // set the focus mode params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); // set Camera parameters mCamera.setParameters(params);

Android - Touch Most mobile devices support touch – single and multi View class supports touch events The base class for touch support is the MotionEvent class which is passed to Views via the onTouchEvent() method To react to touch events you override the onTouchEvent() method The MotionEvent class contains the touch related information the number of pointers the X/Y coordinates size and pressure of each pointer To react to touch events in an activity, register an OnTouchListener for the relevant Views

Android - Touch Touch events

Android - MultiTouch MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP are sent starting with the second finger For the first finger MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP are used getPointerCount() method on MotionEvent allows you to determine the number of pointers on the device To track the touch events from multiple pointers you have to use the MotionEvent.getActionIndex() and the MotionEvent.getActionMasked() methods to identify the index of the pointer and the touch event which happened for this pointer.