Mad Unit 6
Mad Unit 6
Unit :6
Subject Name: MAD Model Answer Subject Code:22617
your app among a select group of users before you make it go live for everyone to
access. This is a safer option because you can analyze the test results and optimize
or fix your app accordingly if you need to before rolling it out to all users. Once
you create a production release, your uploaded app version will become accessible
to everyone in the countries you choose to distribute it in and click on ‘Create
release.’
Step 6: Provide an Appropriate Content Rating If you don’t assign a rating to your
app, it will be listed as ‘Unrated’. Apps that are ‘Unrated’ may get removed from
Google Play. To rate your app, you need to fill out a content rating questionnaire
An appropriate content rating will also help you get to the right audience, which
will eventually improve your engagement rates.
Step 7: Set Up Pricing & Distribution Before you can fill out the details required in
this step, you need to determine your app’s monetization strategy. Once you know
how your app is going to make money, you can go ahead and set up your app as
free or paid. You can always change your app from paid to free later, but you
cannot change a free app to paid. For that, you’ll need to create a new app and set
its price. Step 8: Rollout Release to Publish Your App The final step involves
reviewing and rolling out your release after making sure you’ve taken care of
everything else. Before you review and rollout your release, make sure the store
listing, content rating, and pricing and distribution sections of your app each have a
green check mark next to them. Once you’re sure about the correctness of the
details, select your app and navigate to ‘Release management’ – ‘App releases.’
You can always opt for reviews by clicking on ‘Review’ to be taken to the ‘Review
and rollout release’ screen. Here, you can see if there are any issues or warnings
you might have missed out on. Finally, select ‘Confirm rollout.’ This will also
publish your app to all users in your target countries on Google Play.
e Describe permissions required for android application development. 4M
Ans Declaring and Using Permissions The purpose of a permission is to protect the (2 Marks
privacy of an Android user. Android apps must request permission to access for two
sensitive user data (such as contacts and SMS), as well as certain system features permissi
(such as camera and internet). Depending on the feature, the system might grant the on
permission automatically or might prompt the user to approve the request. explanati
Permissions are divided into several protection levels. The protection level affects on )
whether runtime permission requests are required. There are three protection levels
that affect thirdparty apps: normal, signature, and dangerous permissions.
Normal permissions cover areas where your app needs to access data or resources
outside the app’s sandbox, but where there’s very little risk to the user’s privacy or
the operation of other apps. For example, permission to set the time zone is a
normal permission. If an app declares in its manifest that it needs a normal
permission, the system automatically grants the app that permission at install time.
The system doesn’t prompt the user to grant normal permissions, and users cannot
revoke these permissions.
Signature permissions: The system grants these app permissions at install time,
but only when the app that attempts to use permission is signed by the same
certificate as the app that defines the permission.
Dangerous permissions: Dangerous permissions cover areas where the app wants
data or resources that involve the user’s private information, or could potentially
affect the user’s stored data or the operation of other apps. For example, the ability
to read the user’s contacts is a dangerous permission. If an app declares that it needs
a dangerous permission, the user must explicitly grant the permission to the app.
Until the user approves the permission, your app cannot provide functionality that
depends on that permission. To use a dangerous permission, your app must prompt
the user to grant permission at runtime. For more details about how the user is
prompted, see Request prompt for dangerous permission
f Develop an android application to show current location of an user's car. 4M
Ans activity_maps.xml (2 M for
<fragment xmlns:android="http://schemas.android.com/apk/res/android" xml
xmlns:map="http://schemas.android.com/apk/res-auto" code, 1
xmlns:tools="http://schemas.android.com/tools" M java
android:id="@+id/map" code, 1
android:name="com.google.android.gms.maps.SupportMapFragment" M for
android:layout_width="match_parent" permissi
android:layout_height="match_parent" ons )
tools:context="example.com.mapexample.MapsActivity" />
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;
@Override
@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) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
}
@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(BitmapDescriptorFac
tory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
g State and elaborate the syntax of required class and methods for 4M
Geocoding.
Geocoder: (2 M for
A class for handling geocoding and reverse geocoding. explanati
Geocoding is the process of transforming a street address or other description of a on and
location into a (latitude, longitude) coordinate. syntax of
Reverse geocoding is the process of transforming a (latitude, longitude) coordinate class, 2
into a (partial) address. The amount of detail in a reverse geocoded location M for
description may explanati
vary, for example one might contain the full street address of the closest building, on and
while syntax of
another might contain only a city name and postal code. any two
The Geocoder class requires a backend service that is not included in the core methods)
android
framework.
The Geocoder query methods will return an empty list if there no backend service
in the platform. Use the isPresent() method to determine whether a Geocoder
implementation
exists.
Syntax
Geocoder (Context context)
Constructs a Geocoder localized for the default locale.
Geocoder(Context context, Locale locale)
Constructs a Geocoder localized for the given locale.
Methods with Syntax
a. getFromLocation
Syntax
public List<Address> getFromLocation (double latitude, double longitude, int
maxResults)
public void getFromLocation (double latitude, double longitude, int maxResults,
Geocoder.GeocodeListener listener)
This method returns an array of Addresses that attempt to describe the area
immediately
surrounding the given latitude and longitude. The returned addresses should be
localized
for the locale provided to this class's constructor.
b. getFromLocationName
Syntax :
● public List<Address> getFromLocationName (String locationName, int
maxResults, double lowerLeftLatitude, double lowerLeftLongitude, double
upperRightLatitude, double upperRightLongitude)
● public void getFromLocationName (String locationName, int maxResults,
double lowerLeftLatitude, double lowerLeftLongitude, double
upperRightLatitude, double upperRightLongitude, Geocoder.GeocodeListener
listener)
● public void getFromLocationName (String locationName, int maxResults,
Geocoder.GeocodeListener listener)
● public List<Address> getFromLocationName (String locationName, int
maxResults)
Returns an array of Addresses that attempt to describe the named location, which
may be
a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre
Parkway,
Mountain View, CA", an airport code such as "SFO", and so forth. The returned
addresses should be localized for the locale provided to this class's constructor.
c.isPresent
Syntax
public static boolean isPresent ()
Returns true if there is a geocoder implementation present that may return results. If
true,
there is still no guarantee that any individual geocoding attempt will succeed
h Develop an application to display Google map with user's current location. (Note : 6M
Consider the appropriate XML file. All attributes are not required. In java file all
imports are not expected. Different relevant logic/code can be considered.)
Ans activity_main.xml XML
<?xml version="1.0" encoding="utf-8"?> file: 1M
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" Java
xmlns:app="http://schemas.android.com/apk/res-auto" Code:
xmlns:tools="http://schemas.android.com/tools" 5M
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/google_map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
MainActivity.Java
package com.example.location;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
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;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends FragmentActivity implements
OnMapReadyCallback
{
Location currentlocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();
}
private void fetchLastLocation() {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location!=null)
{
currentlocation=location;
Toast.makeText(getApplicationContext(),currentlocation.getLatitude()+""+current
location.getLongitude(), Toast.LENGTH_SHORT).show();
SupportMapFragment supportMapFragment =
(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.go
ogle_map);
supportMapFragment.getMapAsync(MainActivity.this);
}
}
});
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
LatLng latLng=new
LatLng(currentlocation.getLatitude(),currentlocation.getLongitude());
MarkerOptions markerOptions=new MarkerOptions().position(latLng)
.title("I am Here");
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,5));
googleMap.addMarker(markerOptions);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchLastLocation();
}
break;
}
}
}
i Write a program to find the direction from user's current location to MSBTE, 6M
Bandra. (Write only Java and manitest file). (Note : Any other relevant logic to get
the required output can also be considered.)
Ans AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.msbte.google_map_currentlocationroute">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<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.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<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>
MainActivity.java
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.DexterError;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.PermissionRequestErrorListener;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import java.util.List;
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener , OnMapReadyCallback,
TaskLoadedCallback{
//variables for map and route
private GoogleMap mMap;
private MarkerOptions place1, place2;
Button getDirection;
private Polyline currentPolyline;
private MapFragment mapFragment;
user!", Toast.LENGTH_SHORT).show();
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
// show alert dialog navigating to Settings
openSettingsDialog();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest>
permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Some Error! ",
Toast.LENGTH_SHORT).show();
}
})
onSameThread() .check(); } private void openSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Required Permissions"); builder.setMessage("This app require
permission to use awesome feature. Grant them in app settings.");
builder.setPositiveButton("Take Me To SETTINGS", new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog, int which) { dialog.cancel(); Intent intent = new
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri =
Uri.fromParts("package", getPackageName(), null); intent.setData(uri);
startActivityForResult(intent, 101); } }); builder.setNegativeButton("Cancel", new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.show(); }
//methods for getting current location @Override public void onConnected(Bundle
bundle) { if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) { return; } startLocationUpdates();
mLocation =
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLocation == null){ startLocationUpdates(); } if (mLocation != null) { //
mLatitudeTextView.setText(String.valueOf(mLocation.getLatitude()));
//mLongitudeTextView.setText(String.valueOf(mLocation.getLongitude()));
} else {
Toast.makeText(this, "Location not Detected", Toast.LENGTH_SHORT).show();
}}
@Override
public void onConnectionSuspended(int i) {
new FetchURL(MainActivity.this).execute(getUrl(place1.getPosition(),
place2.getPosition(), "driving"), "driving");
}
});
place1 = new MarkerOptions().position(new LatLng(location.getLatitude(),
location.getLongitude())).title("Location 1");
place2 = new MarkerOptions().position(new
LatLng(19.021824,72.8662016)).title("MSBTE");
mapFragment = (MapFragment)
getFragmentManager().findFragmentById(R.id.mapNearBy);
mapFragment.getMapAsync(this);
isFirstTime = false;
} }}
j Explain Geocoding and Reverse Geocoding with suitable example. 6M
Ans Geocoding is the process of transforming a street address or other description of a (Geocodi
location into a (latitude, longitude) coordinate. Reverse geocoding is the process of ng ,
transforming a (latitude, longitude) coordinate into a (partial) address. The amount Reverse
of detail in a reverse geocoded location description may vary, for example one Geocodi
might contain the full street address of the closest building, while another might ng
contain only a city name and postal code. Explanati
The Geocoder class is used for handling geocoding and reverse geocoding. on : 3 M,
Example
activity_maps.xml :3M)
<fragment xmlns:android="http://schemas.android.com/apk/res/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.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.mapexample.MapsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="248dp"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Search Location" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="searchLocation"
android:text="Search" />
</LinearLayout>
</fragment>
AndroidManifest.xml
<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" />
MapsActivity.java
package example.com.mapexample;
import android.location.Address;
import android.location.Geocoder;
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 android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import java.io.IOException;
import java.util.List;
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback,
LocationListener,GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
LocationRequest mLocationRequest;
@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;
}
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) {
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
mLocationRequest, this);
}
@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(BitmapDescriptorFac
tory.HUE_GREEN));
mCurrLocationMarker = mMap.addMarker(markerOptions);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,
this);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
} catch (IOException e) {
e.printStackTrace();
}
Address address = addressList.get(0);
LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(location));
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
Toast.makeText(getApplicationContext(),address.getLatitude()+"
"+address.getLongitude(),Toast.LENGTH_LONG).show();
}
}
}
k Develop an android application for sending Short Message Service (SMS). 6M
Ans activity_main.xml (XML
<?xml version="1.0" encoding="utf-8"?> file 3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" marks
android:orientation="vertical" android:layout_width="match_parent" Java file
android:layout_height="match_parent"> 3 Marks)
<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"
android:text="Send SMS" />
</LinearLayout>
MainActivity.java
package in.org.msbte.sendsmsexample;
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().to
String(),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();
}
}
});
}
}