Day 6
Upcoming SlideShare
Loading in...5

Day 6



Android internal APIs

Android internal APIs
-> Phone call
-> SMS
-> GMail
-> GPS



Total Views
Views on SlideShare
Embed Views



3 Embeds 437 378 56
http://localhost 3



Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Day 6 Day 6 Presentation Transcript

  • Android APIs: Phone, Camera, SMS, GPS, Email
  • Using Permissions ● ● ● A basic android application has no permission associated with it by default. To make use of the protected features of the device we declare the <uses-permissions> in our manifest file. Permissions are granted by the users when the application is installed, not while it's running.
  • Using Permissions ● Attributes: – android:name ● ● The name of the permission. android.permission.CAMERA, etc. For more: android:maxSdkVersion ● – ● ● The highest API level at which the permission should be granted to your app. Optional to declare.
  • Accessing in-built features ● We always use Intent for this purpose. ● We have separate ids for these features: – – ● Intent.ACTION_VIEW Intent.ACTION_SEND Use of other methods, like: – intent.setType() – intent.setData() are also seen.
  • Phone Call ● Permission: <uses-permission android:name= “android.permission.CALL_PHONE”/> ● Code: Intent i = new Intent(Intent.ACTION_CALL); i.setData(Uri.parse(“tel:980XXXXXXX”)); startActivity(i);
  • Phone Call ● If you just want to direct the user to his/her dialer: Intent i = new Intent(Intent.ACTION_DIAL); startActivity(i); ● No permissions required here.
  • SMS ● Code: Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse("smsto:980XXXXXXX")); startActivity(i); ● No permissions here.
  • SMS ● What if I want to send a message to a user from my application ?? – Use SmsManager. ● – ● Manages SMS operations such as sending data, text SMS messages. Get this object by calling the static method getDefault(). Permission: <uses-permission android:name= “android.permission.SEND_SMS”/>
  • SMS ● private void sendSMS(String phoneNumber, String message) { String SENT = "SMS_SENT"; String DELIVERED = "SMS_DELIVERED"; Message and to which toSend.putExtra("number", phoneNumber); number is it to be sent Intent toSend = new Intent(SENT); PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, toSend, 0); PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); } Actual work is done
  • SMS ● What is PendingIntent ? – Because sending SMS is not our app's function, the Intent call is forwarded to a foreign application (to our default messaging app). – Using pending intents we specify an action to take in the future using the same permissions as your application.
  • sendTextMessage() ● The parameters to provide are: – First parameter: Destination number/address. – Second parameter: Source number/address. – Third parameter: The message to be sent – Forth parameter: Pending intent/sent intent. – Fifth parameter: Pending intent/delivery intent (was the message delivered?)
  • Email ● Permission: <uses-permission= “android.permission.INTERNET”/> ● Coding: Intent i = new Intent(Intent.ACTION_SEND); i.setType("text/plain"); startActivity(i);
  • Email ● Sending the user straight to G-Mail: Intent i = new Intent(Intent.ACTION_SENDTO); i.setType("text/html"); i.setData(Uri.parse("")); startActivity(i); ● Adding preset subject and message: i.putExtra(Intent.EXTRA_SUBJECT, “abc”); i.putExtra(Intent.EXTRA_TEXT, “abc”);
  • Email ● Sending the user straight to G-Mail: Intent i = new Intent(Intent.ACTION_SENDTO); i.setType("text/html"); i.setData(Uri.parse("")); startActivity(i); ● Adding preset subject and message: i.putExtra(Intent.EXTRA_SUBJECT, “abc”); i.putExtra(Intent.EXTRA_TEXT, “abc”);
  • Email ● Sending the user straight to G-Mail (simple): i.setClassName("", "");
  • Camera : Permissions ● Camera Permissions* – Use the device <uses-permission android:name= “android.permission.CAMERA”/> ● Camera Features – Use camera features <uses-permission android:name= “”/>
  • Camera : Permissions ● Storage Permissions* – The application saves images/videos to the device's external storage <uses-permission android:name= “android.permission.WRITE_EXTERNAL_STORAGE”/>
  • Camera : Coding ● Our goal here is to display the photo taken by the user when the camera was called.
  • Camera : Coding ● Logic – Because the shot photo is to be called back into the activity: ● ● ● – Set up a directory for the image. Get the output media file from that directory. startActivityForResult() is to be called. Use of Bitmap to show the image captured.
  • Global Variables // Activity request codes private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100; public static final int MEDIA_TYPE_IMAGE = 1; // directory name to store the captured images private static final String IMAGE_DIRECTORY_NAME = "my_camera_app"; // file url to store image private Uri fileUri;
  • Camera : Coding ● Taking a picture: private void captureImage() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // start the image capture Intent startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); } Instantiated using helper methods
  • Activity for result ● ● ● A method that is used when we would like to receive something Launch an activity and listen for results when finished When this activity exits, your onActivityResult() method will be called with the given requestCode
  • Activity for result protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) { if (resultCode == RESULT_OK) { // Successfully captured the image display in imageview previewImage(); } else { // failed to capture image Toast.makeText(getApplicationContext(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show(); } } }
  • previewImage() private void previewImage() { try { // Bitmap factory BitmapFactory.Options options = new BitmapFactory.Options(); // Downsizing image as it throws OutOfMemory exception for larger images options.inSampleSize = 5; final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options); photo.setImageBitmap(bitmap); } catch (NullPointerException e) { e.printStackTrace(); } }
  • Helper method ● Get media file: – Setting the external sdcard location. – Checking on the storage directory(if it does not exist). – Naming the media. – Returning the media. private static File getOutputMediaFile(int type) { ... return mediaFile; }
  • Get the media file ● Setting up the external sdcard location: File mediaStorageDir = new File(Environment.getExternalStoragePublicDirec tory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME); Directory path Directory name to store captured image, declared as a global variable.
  • Get the media file ● Checking on the storage directory, if it does not exist: if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "+ IMAGE_DIRECTORY_NAME + " directory"); return null; } }
  • Get the media file ● Naming the media File mediaFile; if (type == MEDIA_TYPE_IMAGE) { mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + ".jpg"); } else { return null; }
  • Get the media file ● ● Finally, return the mediaFile. Because this file is referred to from an URI, the file is to be converted to an URI. public Uri getOutputMediaFileUri(int type) { return Uri.fromFile(getOutputMediaFile(type)); } Full code: For further reference:
  • GPS ● Permission: <uses-permission android:name= “android.permission.ACCESS_FINE_LOCATION”/> ● Coding – – Objective : Check if GPS is on or not. ● Call on the GPS settings intent. If on, show coordinates. ● Call the location manager.
  • Call on the GPS settings ● Intent GPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATI ON_SOURCE_SETTINGS);
  • LocationManager ● ● This class provides access to the system location services. These services allow applications: – – ● to obtain periodic updates of the device's geographical location. to fire an application-specified Intent when the device enters the proximity of a given geographical location. We instantiate LocationManager through: Context.getSystemService(Context.LOCATION_SERVICE)
  • LocationManager ● Checking to see if the location provider is enabled or not: if(locationManager.isProviderEnabled(LocationMana ger.GPS_PROVIDER)) ● Fetch the location using the getLastKnowLocation(): Location location = locationManager.getLastKnownLocation(LocationMana ger.GPS_PROVIDER); double latitude = location.getLatitude(); double longitude = location.getLongitude(); Full code: