Mobile Application Development Chapter 6
Mobile Application Development Chapter 6
Mobile Application Development Chapter 6
Package is
android.provider.Telephony.Sms
Contains all text-based SMS messages.
Nested Class
Telephony.Sms.Draft
Contains all draft text-based SMS messages in the SMS app.
Telephony.Sms.Inbox
Contains all text-based SMS messages in the SMS app inbox.
Telephony.Sms.Intents
Contains constants for SMS related Intents that are broadcast.
Telephony.Sms.Outbox
Contains all pending outgoing text-based SMS messages.
Telephony.Sms.Sent
Contains all sent text-based SMS messages in the SMS app.
Telephony.Sms.Conversations
Contains a view of SMS conversations (also referred to as threads).
• send SMS from our android application in two
ways either by using SMSManager API
or Intents .
• If we use SMSManager API, it will directly
send SMS from our application. In case if we
use Intent with proper action (ACTION_VIEW),
it will invoke a built-in SMS app to send SMS
from our application.
1.Android Send SMS using
SMSManager API
• SmsManager smgr = SmsManager.getDefault();
smgr.sendTextMessage(MobileNumber,null,Message,n
ull,null);
• <uses-permission android:name="android.permission.S
END_SMS"/>
•
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Mobile No" />
<EditText
android:id="@+id/mblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
• <TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
MainActivity.java
import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText txtMobile;
private EditText txtMessage;
private Button btnSms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMobile = (EditText)findViewById(R.id.mblTxt);
• txtMessage = (EditText)findViewById(R.id.msgTxt);
btnSms = (Button)findViewById(R.id.btnSend);
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
SmsManager smgr = SmsManager.getDefault();
smgr.sendTextMessage(txtMobile.getText().toString(),null,txtMessage.getText().toSt
ring(),null,null);
Toast.makeText(MainActivity.this, "SMS Sent Successfully",
Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "SMS Failed to Send, Please try again",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
AndroidManifest.xml
• <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutlane.sendsmsexample">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.Android Send SMS using Intent
• In android, Intent is a messaging object which is used to
request an action from another app component such
as activities, services, broadcast receivers, and content
providers.
• To send SMS using the Intent object
Intent sInt = new Intent(Intent.ACTION_VIEW);
sInt.putExtra("address", new String[]{txtMobile.getText().toString
()});
sInt.putExtra("sms_body",txtMessage.getText().toString());
sInt.setType("vnd.android-dir/mms-sms");
• <uses-permission android:name="android.permission.SEND
_SMS"/>
btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("smsto:"));
i.setType("vnd.android-dir/mms-sms");
i.putExtra("address", new String(txtMobile.getText().toStrin
g()));
i.putExtra("sms_body",txtMessage.getText().toString());
startActivity(Intent.createChooser(i, "Send sms
via:"));
}
catch(Exception e){
Toast.makeText(MainActivity.this, "SMS Failed to
Send, Please try again", Toast.LENGTH_SHORT).show();
}
Receive SMS
• Receive SMS Permissions
– We only need receive
permission android.permission.RECEIVE_SMS. In
case you also want to read SMS messages from the
Inbox then you need android.permission.READ_SMS.
Intent Filter to receive SMS
• We also need to tell Android that we want to handle
incoming SMS messages. In order to do this, we will add
a <receiver> to register a broadcast receiver to the manifest
XML. We will also add an <intent-filter> to let Android know
that we want to launch a specific class when an SMS comes
in.
<receiver
android:name="com.javarticles.android.SMSReceiver">
<intent-filter>
<action
android:name="android.provider.Telephony.SMS_RECEIVED"
/>
</intent-filter>
Broadcast Receiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.SmsMessage;
@Override
public void onReceive(Context context, Intent intent) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
Activity_main.xml
<LinearLayout 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:orientation="vertical"
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="com.javarticles.android.DatePickerExample" >
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="18dp"
android:text="@string/welcome"
android:textColor="@color/welcome_text_color"
android:textSize="20sp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.javarticles.android"
android:versionCode="1"
android:versionName="1.0" >
AndroidManifest.xml:
<uses-sdk
android:maxSdkVersion="22"
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher_sandbox"
android:label="@string/app_name" >
<activity android:name="com.javarticles.android.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.javarticles.android.SMSReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Email
• To compose an email, use one of the below actions based on whether
you'll include attachments, and include email details such as the
recipient and subject using the extra keys listed below.
• ActionACTION_SENDTO (for no attachment) or
• ACTION_SEND (for one attachment) or
• ACTION_SEND_MULTIPLE (for multiple attachments)
• MIME Type
– "text/plain"“
– */*“
• Extras
– Intent.EXTRA_EMAIL A string array of all "To" recipient email addresses.
– Intent.EXTRA_CC A string array of all "CC" recipient email addresses.
– Intent.EXTRA_BCC A string array of all "BCC" recipient email addresses
– Intent.EXTRA_SUBJECT A string with the email subject.
– Intent.EXTRA_TEXT A string with the body of the email.
– Intent.EXTRA_STREAM A Uri pointing to the attachment. If using
the ACTION_SEND_MULTIPLE action, this should instead be
an ArrayList containing multiple Uri objects.
• public void composeEmail(String[] addresses, String
subject, Uri attachment) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM,
attachment);
if (intent.resolveActivity(getPackageManager()) !=
null) {
startActivity(intent);
}
}
• If you want to ensure that your intent is handled only by an email
app (and not other text messaging or social apps), then use
the ACTION_SENDTO action and include the "mailto:" data scheme.
• For example:
public void composeEmail(String[] addresses, String subject)
{
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should
handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
if (intent.resolveActivity(getPackageManager()) != null)
{
startActivity(intent);
}
}
• <activity ...>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:type="*/*" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SENDTO"
/>
<data android:scheme="mailto" />
<category
android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:androclass="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=".MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="22dp"
android:layout_marginTop="18dp"/>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/editText1"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/editText2" />
<TextView
android:id="@+id/textView1"
android:textColor="#0F9D58"
android:text="Send To:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"/> <TextView
android:id="@+id/textView2"
android:textColor="#0F9D58"
android:text="Email Subject:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/textView3"
android:textColor="#0F9D58"
android:text="Email Body:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"/>
<Button
android:id="@+id/button"
android:text="Send email!!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/editText3"
android:layout_marginLeft="76dp"
android:layout_marginTop="20dp"/> </RelativeLayout>
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.net.Uri;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onClick(View view)
{
String emailsend = sendto.getText().toString();
String emailsubject = subject.getText().toString();
String emailbody = body.getText().toString();
Generally, the Intent object in android with proper action (ACTION_CALL) and data will
help us to launch a built-in phone calls app to make a phone calls in our application.
In android, Intent is a messaging object which is used to request an action from another
app component such as activities, services, broadcast receivers, and content
providers. To know more about an Intent object in android check this Android
Intents with Examples.
To make a phone call using Intent object in android application, we need to write the
code like as shown below.
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Android Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:androclass="http://schemas.android.com/apk/res/android"
package="com.example.phonecall"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
</manifest>
Location Based Services
• googleMap.setMapType(GoogleMap.MAP_TY
PE_NORMAL);
• googleMap.setMapType(GoogleMap.MAP_TY
PE_HYBRID);
• googleMap.setMapType(GoogleMap.MAP_TY
PE_SATELLITE);
• googleMap.setMapType(GoogleMap.MAP_TY
PE_TERRAIN);
Methods of Google map
Methods Description
addCircle(CircleOptions options) This method add circle to map.
addPolygon(PolygonOptions options) This method add polygon to map.
addTileOverlay(TileOverlayOptions This method add tile overlay to the map.
options)
animateCamera(CameraUpdate This method moves the map according to
update) the update with an animation.
clear() This method removes everything from the
map.
getMyLocation() This method returns the currently displayed
user location.
moveCamera(CameraUpdate update) This method reposition the camera according
to the instructions defined in the update.
setTrafficEnabled(boolean enabled) This method set the traffic layer on or off.
snapshot(GoogleMap.SnapshotReadyC This method takes a snapshot of the map.
allback callback)
stopAnimation() This method stops the camera animation if
there is any progress.
Example of Google Map
Copy the URL from
google_map_api.xml file to generate
Google map key.
Paste the copied URL at the browser. It
will open the following page.
Click on Create API key to generate API
key.
After clicking on Create API key, it will
generate our API key displaying the
following screen.
Copy this generated API key in
our google_map_api.xml file
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/r
es/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMap
Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity" />
MapsActivity.java
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.mapexample">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /
>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
build.gradel
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:2
6.1.0'
implementation 'com.google.android.gms:play-services-
maps:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:
runner:1.0.1'
androidTestImplementation 'com.android.support.test.
espresso:espresso-core:3.0.1'
}
Android Google Map Displaying
Current Location
• Callback methods in Google Map
• OnMapRreadyCallback: This callback interface invokes when it
instance is set on MapFragment object. The onMapReady(GoogleMap)
method of OnMapReadyCallback interface is called when the map is
ready to used. In the onMapReady(GoogleMap) method we can add
markers, listeners and other attributes.
• LocationListener: This interface is used to receive notification when
the device location has changed. The abstract method of
LocationListener onLocationChanged(Location) is called when the
location has changed.
• GoogleApiClient.ConnectionCallbacks: This interface provide
callbacks methods onConnected(Bundle) and
onConnectionSuspended(int) which are called when the device is to
connected and disconnected.
• GoogleApiClient.OnConnectionFailedListener: This interface provide
callbacks method onConnectionFailed(ConnectionResult) which is
called when there was an error in connecting the device to the
service.
• The setMyLocationEnabled() method of GoogleMap is used to enable
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/r
es/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMap
Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity" />
build.gradel
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-maps:11.8.
0'
compile 'com.google.android.gms:play-services-location:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.
1'
androidTestImplementation 'com.android.support.test.espresso:es
presso-core:3.0.1'
}
MapsActivity.java
import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.location.LocationServices;
import android.location.Location;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
public class MapsActivity extends FragmentActivity implements OnMapRead
yCallback,
LocationListener,GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is rea
dy to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSuppo
rtFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
@Override
public void onConnectionSuspended(int i) {
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
<?xml version="1.0" encoding="utf-8"?>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.mapexample">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Zoom Controls
• The Zoom Controls class displays a simple set of
controls used for zooming and provides callbacks
to register for events.
• Xml code
<ZoomControls android:id="@+id/zoomControls1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
public class MainActivity extends Activity
{
ZoomControls zoom;
Button hide,show;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
zoom = (ZoomControls) findViewById(R.id.zoomControls1);
hide = (Button) findViewById(R.id.button1);
show = (Button) findViewById(R.id.button2);
show.setOnZoomInClickListener(new OnClickListener() { @Override public void onClick(View v) {
zoom.show();
}};
LocationManager locationManager =
(LocationManager)getSystemService(Context.LOC
ATION_SERVICE);
Step 2
• identify the location provider you would like to use for retrieving location details.
The location providers actually provide the location data in Android. Android
exposes two main location providers:
– A. It is significantly slower than the network provider to get the initial connection setup
with the satellites. This initial connection, also called Time to First Fix (TTFF) can be
extremely slow but once the connection is established, location updates are quite fast
– B. GPS provider also drains out the battery very first. So, it is very important to judiciously
use this provider. Special care should be taken to ensure that this is turned off when you
are not using it.
– C. The third draw back comes from the fact that GPS uses radio signal which is obstructed
by solid objects. So, GPS provider will not work if you are inside a building or in a basement.
2. Network Provider:
This provider uses Wi-Fi hotspots and cell tower to approximate a location. The
provider retrieves an approximate location by querying the Google location server
using the IDs of the Wi-Fi hotspots and cell towers. Location accuracy is between 100
– 1000 m. The location accuracy is directly proportional to the number of Wi-Fi
hotspots and cell towers available in the area.
You can check the availability of the providers
using the below piece of code:
isGpsEnabled = mLocManager.isProviderEnab
led(LocationManager.GPS_PROVIDER);
isNetworkEnabled =
mLocManager.isProviderEnabled(LocationMa
nager.NETWORK_PROVIDER);
Step 3
To register a location listener with the location manager.
The registration can be done by calling the
method requestLocationUpdates((String provider, long minTime, float
minDistance, LocationListener listener).
Following are the parameters passed to this method:
– provider:
the name of the provider with which to register. This can be either GPS or
Network provider.
– 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.
– Leaking Information to Logs: Android provides centralized logging via the Log API,
which can displayed with the “logcat” command. While logcat is a debugging tool,
applications with the READ_LOGS permission can read these log messages. The
Android documentation for this permission indicates that “the logs can contain
slightly private information about what is happening on the device, but should never
contain the user’s private information.”
– SDcard Use: Any application that has access to read or write data on the SDcard
can read or write any other application’s data on the SDcard.
– Intent Injection Attacks: Intent messages are also used to start activity and service
components. An intent injection attack occurs if the in-tent address is derived from
untrusted input.
– Wifi Sniffing: This may disrupt the data being transmitted from A device like many
web sites and applications does not have security measures strict security. The
application does not encrypt the data and therefore it can be Blocked by a listener
on unsafe lines.
Requesting permissions