Chapter 6
Chapter 6
SMS telephony
In Android, you can use SmsManager API or devices Built-in SMS application to send
SMS's
Android SMS is stored in PDU (protocol description unit) format
SmsManager class takes care of sending the SMS message.
We just need to get an instance of it and send the SMS message.
We need to add permission to SEND_SMS in the Android manifest file.
<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/Theme.IFCDiv">
<activity android:name=".ttp"></activity>
<activity android:name=".login" />
<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>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="enter phone no"
android:id="@+id/ed1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="enter msg"
android:layout_below="@id/ed1"
android:id="@+id/ed2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="@+id/b1"
android:layout_below="@id/ed2"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
package com.example.contentpro;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.ed1);
ed2 = findViewById(R.id.ed2);
b = findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checkPermission()) {
sendMessage();
} else {
requestPermission();
}
}
});
}
Receive SMS
activity_main.xml:
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AndroidManifest.xml:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
SMSReceiver.java:
package com.example.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
@Override
public void onReceive(Context context, Intent intent) {
try {
if
(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus != null) {
for (Object pdu : pdus) {
SmsMessage smsMessage =
SmsMessage.createFromPdu((byte[]) pdu);
String sender =
smsMessage.getDisplayOriginatingAddress();
String messageBody = smsMessage.getMessageBody();
Toast.makeText(context, "SMS received from: " + sender +
"\nMessage: " + messageBody, Toast.LENGTH_LONG).show();
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Email is messages distributed by electronic means from one system user to one or more
recipients via a network.
To send an email from your application, you don’t have to implement an email client
from the beginning, but you can use an existing one like the default Email app provided
from Android, Gmail, Outlook, K-9 Mail etc.
For this purpose, we need to write an Activity that launches an email client, using an
implicit Intent with the right action and data.
We can use ACTION_SEND action to launch an email client installed on your Android
device.
Following is simple syntax to create an intent with ACTION_SEND action.
Example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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="16dp"
android:ems="10" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="18dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="28dp"
android:ems="10"
android:inputType="textMultiLine" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="To:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Subject:" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText3"
android:layout_alignBottom="@+id/editText3"
android:layout_alignParentLeft="true"
android:text="Message:" />
<Button
android:id="@+id/button1"
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"
android:text="Send" />
</RelativeLayout>
mainActivity.java file
public class MainActivity extends AppCompatActivity
{
EditText editTextTo,editTextSubject,editTextMessage;
Button send;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTo=(EditText)findViewById(R.id.editText1);
editTextSubject=(EditText)findViewById(R.id.editText2);
editTextMessage=(EditText)findViewById(R.id.editText3);
send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
String to=editTextTo.getText().toString();
String subject=editTextSubject.getText().toString();
String message=editTextMessage.getText().toString();
Most security between applications and the system is enforced at the process level
through standard Linux facilities, such as user and group IDs that are assigned to
applications.
The Android security model is primarily based on a sandbox and permission mechanism.
Each application is running in a specific Dalvik virtual machine with a unique user ID
assigned to it, which means the application code runs in isolation from the code of all
others applications.
Android application has been signed with a certificate with a private key Know the
owner of the application is unique.
When an application is installed in The phone is assigned a user ID, thus avoiding it from
affecting it Other applications by creating a sandbox for it. This user ID is permanent on
which devices and applications with the same user ID are allowed to run in a single
process.
It is mandatory for an application to list all the resources it will Access during
installation.
The purpose of a permission is to protect the privacy of an Android user. Android apps
must request permission to access sensitive user data (such as contacts and SMS), as well
as certain system features (such as camera and internet).
Permissions are divided into several protection levels. The protection level affects
whether runtime permission requests are required.
Android introduced shared user ID & permission to allow application components talk to each
Shared User ID
Shared user ID enables Android system to share data between application
components.
Both applications need to be signed with same digital certificate in order to be
assigned a shared user ID as shown in image.
Developers will able to pass through the restriction on isolation model & both
applications will gain access to run in the same process.
Types of permissions
1. Install-time permissions
Install-time permissions give your app limited access to restricted data, and they
allow your app to perform restricted actions that minimally affect the system or other
apps.
When you declare install-time permissions in your app, the system automatically
grants your app the permissions when the user installs your app.
Android includes several sub-types of install-time permissions,
a) Normal permissions
These permissions allow access to data and actions that extend beyond your app's
sandbox.
However, the data and actions present very little risk to the user's privacy, and the
operation of other apps.
b) Signature permissions
If the app declares a signature permission that another app has defined, and if the two
apps are signed by the same certificate, then the system grants the permission to the
first app at install time. Otherwise, that first app cannot be granted the permission.
2. Runtime permissions
Runtime permissions, also known as dangerous permissions, give your app additional
access to restricted data, and they allow your app to perform restricted actions that
more substantially affect the system and other apps.
Many runtime permissions access private user data, a special type of restricted data
that includes potentially sensitive information. Examples of private user data include
location and contact information.
3. Special permissions
The Special app access page in system settings contains a set of user-toggleable operations.
Many of these operations are implemented as special permissions.
The purpose of a permission is to protect the privacy of an Android user. Android apps
must request permission to access sensitive user data (such as contacts and SMS), as well
as certain system features (such as camera and internet).
Depending on the feature, the system might grant the permission automatically or might
prompt the user to approve the request.
Permission Approval
An app must publicize the permissions it requires by including <uses-permission> tags in
the app manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.snazzyapp">
<uses-permission android:name="android.permission.SEND_SMS"/>
<application ...>
...
</application>
</manifest>
If your app lists normal permissions in its manifest the system automatically grants those
permissions to your app.
If your app lists dangerous permissions in its manifest such as the SEND_SMS permission
above, the user must explicitly agree to grant those permissions.
Only dangerous permissions require user agreement. The way Android asks the user to
grant dangerous permissions depends on the version of Android running on the user's
device, and the system version targeted by your app.
1. Runtime requests
2. Install-time requests
Some apps depend on access to sensitive user information related to call logs and SMS
messages.
If you want to request the permissions specific to call logs and SMS messages and
publish your app to the Play Store, you must prompt the user to set your app as
the default handler for a core system function before requesting these runtime
permissions.
Access to some hardware features (such as Bluetooth or the camera) require an app
permission.
<uses-feature android:name="android.hardware.camera" android:required="false" />
By defining custom permissions, an app can share its resources and capabilities with other
apps.
App signing
All APKs must be signed with a certificate whose private key is held by their developer.
This certificate identifies the author of the app.
The certificate does not need to be signed by a certificate authority; it is perfectly
allowable, and typical, for Android apps to use self-signed certificates.
The purpose of certificates in Android is to distinguish app authors.
User IDs and file access
At install time, Android gives each package a distinct Linux user ID.
The identity remains constant for the duration of the package's life on that device.
On a different device, the same package may have a different UID; what matters is that
each package has a distinct UID on a given device.
<permission
android:name="com.example.myapp.permission.DEADLY_ACTIVITY"
android:label="@string/permlab_deadlyActivity"
android:description="@string/permdesc_deadlyActivity"
android:permissionGroup="android.permission-group.COST_MONEY"
android:protectionLevel="dangerous" />
...
</manifest>
LBS app follows the location and offer extra services such as locating facilities close at
had .
In LBS application map is the key components, which present the visual location of our
location.
Geocoding is the process of transforming a street address or other description of a
location into a (latitude, longitude) coordinate
Creating the Project
Getting the Maps API Key
4. Add your project name and organization name in the fields present on the screen.
5. Now click on APIs and Services.
9. Click on the API key option to generate your API key. After clicking on this option
your API key will be generated
Displaying the Maps
1. Create new android project.
2. Select Google Maps Activity and then click finish.
3. Now open the google_maps_api_xml file and copy the api key
4. Activity_maps.xml file
5. AndroidManifest.xml file
<uses-permission
android:name="android.permission.ACCESS_FINE_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.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>
6. Activity_maps.xml file
package com.example.googlemap;
import androidx.fragment.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;
7. Output
Geocoder
Signing of Application
Application signing allows developers to identify the author of the application and to
update their application without creating complicated interfaces and permissions.
Every application that is run on the Android platform must be signed by the
developer.
Applications that attempt to install without being signed will be rejected by either
Google Play or the package installer on the Android device.
On Android, application signing is the first step to placing an application in its
Application Sandbox. The signed application certificate defines which user ID is
associated with which application; different applications run under different user IDs.
Application signing ensures that one application cannot access any other application
except through well-defined IPC.
There is a one-time fee of $25 by which a developer can open an account, loaded with
functions and control features.
After paying this one-time fee, you can upload apps to Google Play Store for free.
You need to fill out all the credentials asked while creating the account, such as your
name, country and more. Once you submit your account it will take upto 48 hours to get
approved.
If the app getting uploaded to Play Store supports in-app purchases, then you will need a
merchant account.
To create one you can sign in to your Google Console account and click
on ‘Reports’ followed by ‘Financial Reports’ option.
After this, you may select ‘Set up a merchant account now’ option and simply fill out
your details.
The merchant account will automatically get linked to your Google Console account and will
allow you to manage and examine app sales.
3. Create Application
Once you are logged into your developer or publisher account, here are a few steps you need
to take:
You are required to fill out all the information and details you have already prepared
with caution before. The table below shows what information you need to fill in the app
listing-
Make sure to use appropriate keywords in your app description to increase the chances of
your app showing up in searches. Along with this, make sure to use all the data we have
talked about in the prerequisite section for app listing.
This is how you do it: Navigate to the ‘Release Management’ and then ‘App
Release’ tab in the menu.
After this, you will be asked to choose any one type of release from four options- internal
test, close test, production release, and an open test.
Once, you have made a decision regarding which type of release you want, you may
select ‘Create Release’.
At this point, you will be redirected to the New release to the production page. Here, you
are again required to make another decision- to opt for Google Play app signing on the
app or not. If you choose the latter, then simply click on the ‘OPT-OUT’ option.
Now, select ‘Browse files’ and then look into how to upload apk to google
play store while naming and describing your release through on-screen instructions.
You can also click on ‘Review’ to confirm the information. When everything is taken
care of, press ‘Save’.
The next step regarding how to publish apps on the Play Store is to rate your app. This is
crucial for it is listed as ‘Unrated’, it might get removed altogether from the store, so it is
imperative to rate the application.
For Content Rating, you must again navigate to the menu on the left side of the screen
and then select the same. By clicking on ‘Continue’ you can move forward and then
type your email address in the respective field and then ‘Confirm’ it.
Now, you may fill the questionnaire for your app rating. Follow this by selecting
the ‘Save Questionnaire’ and then choose the ‘Calculate Rating’ option to see your
app rating on the Play Store. The last thing to finalize your app’s content rating is to click
on ‘Apply’.
Moreover, assigning a price to your app is crucial. If you want your app to be free, make
sure that this decision is permanent, as Google does not allow you to convert free apps
into paid ones. Although, the price of the app can be altered.
To do all this, go to the Pricing and Distribution tab in the menu, and then make a
choice whether your app is going to be Free or Paid. You may now select the countries
you want your app to be released. Additionally, if your application is suited for children
under the age of 13, you may select the option of ‘Yes’ for Primary Child-Detected. If
otherwise is the case, simply select ‘No’. Similarly, select the options for allowing ads
into your application.
Once you are confirmed about everything being correct, take the last step of this guide
on how to upload an app on Play Store, i.e, add the application to the platform.
You need to go back to the ‘App Releases’ tab and then select ‘Manage Production’
followed by ‘Edit Release’. After this, click on ‘Review’ and then choose ‘Start rollout to
production’ option. To bring this process to an end select the ‘Confirm’ option and
Voila! You have successfully uploaded the app to the Google Play Store for free.
All there is left to do now is to just wait for your application to get approved. It generally
took about two hours for your application to get reviewed. But with Google Play’s
updated privacy policy, it will now take hours and even days for the same,
encouraging mobile app development companies to create even more flawless
applications that get selected instantly. So, hold your excitement in the place and just
wait.
Step-by-Step Process to Upload App To Google Play Store
Different Permission
For CAMERA
<uses-permission android:name="android.permission.CAMERA" />
Your application must also declare use of camera features, for example:
<uses-feature android:name="android.hardware.camera" />
Storage Permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Location Permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Internet Permission
<uses-permission android:name="android.permission.INTERNET" />