0% found this document useful (0 votes)
14 views10 pages

Chapter 6

The document discusses SMS telephony in Android, detailing two methods for sending SMS: using the SMSManager API for direct sending and Intents to invoke the built-in SMS app. It also outlines the Android security model, emphasizing the sandboxing and permission system that protects user data and privacy. Additionally, it provides code examples for obtaining the current location and performing geocoding operations in an Android application.

Uploaded by

koliharsha600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Chapter 6

The document discusses SMS telephony in Android, detailing two methods for sending SMS: using the SMSManager API for direct sending and Intents to invoke the built-in SMS app. It also outlines the Android security model, emphasizing the sandboxing and permission system that protects user data and privacy. Additionally, it provides code examples for obtaining the current location and performing geocoding operations in an Android application.

Uploaded by

koliharsha600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CHAPTER 6

SMS Telephony

→ In android, we can send SMS from our android application in two ways either by using
SMSManager API or Intents based on our requirements.
→ 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.
→ Classes of SMS Telephony

1. Telephony.Sms.Conversations Contains a view of SMS conversations (also referred to as


threads).

2. Telephony.Sms.Draft Contains all draft text-based SMS messages in the SMS app.

3. Telephony.Sms.Inbox Contains all text-based SMS messages in the SMS app inbox.

4. Telephony.Sms.Intents Contains constants for SMS related Intents that are broadcast.

5. Telephony.Sms.Outbox Contains all pending outgoing text-based SMS messages.

6. Telephony.Sms.Sent Contains all sent text-based SMS messages in the SMS app.

Example of sms-

Sms receiver-
Android security model-

→ 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 other applications.
→ Therefore, one application has not granted access to other applications’ files.
→ Android application has been signed with a certificate with a private key Know the owner of the
application is unique.
→ This allows the author of the application will be identified if needed. 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.
→ This is a way to ensure that a malicious application has Cannot access / compromise the data of
the genuine application.
→ It is mandatory for an application to list all the resources it will Access during installation.
Terms are required of an application, in the installation process should be user-based or
interactive Check with the signature of the application
→ Android permissions help protect user privacy by controlling access to sensitive data and
system features. Apps must request permission to access elements like contacts, SMS, camera,
or internet. Permissions are categorized into three levels:
− Normal: Granted automatically at install time, as they pose minimal risk (e.g., setting
time zone).
− Signature: Given only if the requesting app is signed with the same certificate as the app
defining the permission.
− Dangerous: Requires explicit user approval since they involve personal data or could
impact stored data (e.g., reading contacts). Apps must prompt users for these at runtime.

LOCATION-

Code to get currant location-


activity_main.xml
<?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">
<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>

JAVA-
package example.com.mapexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {


private GoogleMap mMap;
private final int LOC_REQ = 1; // Permission request code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps); // Load the map layout

// Get the map fragment and request async map load


((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMapAsync(this);
}

// Called when the map is ready


@Override
public void onMapReady(GoogleMap map) {
mMap = map;

// Check for location permission


if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Ask for permission if not granted
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOC_REQ);
} else {
showLocation(); // Show location if permission already granted
}
}

// Enable "My Location" and place a marker at current location


private void showLocation() {
mMap.setMyLocationEnabled(true); // Show blue dot
LocationServices.getFusedLocationProviderClient(this)
.getLastLocation().addOnSuccessListener(loc -> {
if (loc != null) {
LatLng pos = new LatLng(loc.getLatitude(), loc.getLongitude());
mMap.addMarker(new MarkerOptions().position(pos).title("You are here"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, 15)); // Zoom to
location
}
});
}

// Handle permission result


@Override
public void onRequestPermissionsResult(int req, String[] perms, int[] grants) {
if (req == LOC_REQ && grants.length > 0 &&
grants[0] == PackageManager.PERMISSION_GRANTED) {
showLocation(); // If granted, show location
}
}
}

Required in AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
GEOCODING-
Manifest.xml-
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Layout (res/layout/activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:padding="16dp">

<Button android:id="@+id/btnGeocode"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Get Coordinates" />

<TextView android:id="@+id/tvGeocodeResult"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="10dp" />

<Button android:id="@+id/btnReverseGeocode"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Get Address" android:layout_marginTop="20dp" />

<TextView android:id="@+id/tvReverseGeocodeResult"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="10dp" />
</LinearLayout>

Java Code (MainActivity.java)


package com.example.geocodingdemo;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btnGeocode = findViewById(R.id.btnGeocode);


Button btnReverseGeocode = findViewById(R.id.btnReverseGeocode);
TextView tvGeo = findViewById(R.id.tvGeocodeResult);
TextView tvReverse = findViewById(R.id.tvReverseGeocodeResult);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());

btnGeocode.setOnClickListener(v -> {
try {
List<Address> list = geocoder.getFromLocationName("123 Main St, Mumbai", 1);
if (list != null && !list.isEmpty()) {
Address loc = list.get(0);
tvGeo.setText("Coordinates: " + loc.getLatitude() + ", " + loc.getLongitude());
} else {
tvGeo.setText("Address not found");
}
} catch (IOException e) {
showError(e);
}
});

btnReverseGeocode.setOnClickListener(v -> {
try {
List<Address> list = geocoder.getFromLocation(19.0760, 72.8777, 1);
if (list != null && !list.isEmpty()) {
tvReverse.setText("Address: " + list.get(0).getAddressLine(0));
} else {
tvReverse.setText("Address not found");
}
} catch (IOException e) {
showError(e);
}
});
}

private void showError(IOException e) {


e.printStackTrace();
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}}

You might also like