2. 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.
3. Using Permissions
●
Attributes:
–
android:name
●
●
The name of the permission.
android.permission.CAMERA, etc.
For more: http://bit.ly/19g9vw0
android:maxSdkVersion
●
–
●
●
The highest API level at which the permission should be
granted to your app.
Optional to declare.
4. 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.
6. 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.
7. SMS
●
Code:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("smsto:980XXXXXXX"));
startActivity(i);
●
No permissions here.
8. 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”/>
9. 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
10. 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.
11. 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?)
13. Email
●
Sending the user straight to G-Mail:
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("text/html");
i.setData(Uri.parse("mailto:xxx@abc.com"));
startActivity(i);
●
Adding preset subject and message:
i.putExtra(Intent.EXTRA_SUBJECT, “abc”);
i.putExtra(Intent.EXTRA_TEXT, “abc”);
14. Email
●
Sending the user straight to G-Mail:
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setType("text/html");
i.setData(Uri.parse("mailto:xxx@abc.com"));
startActivity(i);
●
Adding preset subject and message:
i.putExtra(Intent.EXTRA_SUBJECT, “abc”);
i.putExtra(Intent.EXTRA_TEXT, “abc”);
15. Email
●
Sending the user straight to G-Mail (simple):
i.setClassName("com.google.android.gm",
"com.google.android.gm.ConversationListActivity");
16. Camera : Permissions
●
Camera Permissions*
–
Use the device
<uses-permission
android:name= “android.permission.CAMERA”/>
●
Camera Features
–
Use camera features
<uses-permission
android:name= “android.hardware.camera”/>
17. Camera : Permissions
●
Storage Permissions*
–
The application saves images/videos to the
device's external storage
<uses-permission
android:name=
“android.permission.WRITE_EXTERNAL_STORAGE”/>
18. Camera : Coding
●
Our goal here is to
display the photo
taken by the user
when the camera
was called.
19. 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.
20. 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;
21. 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
22. 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
23. 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();
}
}
}
24. 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();
}
}
25. 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;
}
26. 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.
27. 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;
}
}
28. 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;
}
29. 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: http://bit.ly/1gIVSOS
For further reference: http://bit.ly/1bSk4MH
31. Call on the GPS settings
●
Intent GPSSettingIntent = new
Intent(android.provider.Settings.ACTION_LOCATI
ON_SOURCE_SETTINGS);
32. 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)
33. 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: http://bit.ly/1i8YyEs