Professional Documents
Culture Documents
Unit 3 Cyber Laws
Unit 3 Cyber Laws
Unit 3 Cyber Laws
Telephony APIs
9.1 Working with Telephony Utilities, Using SMS, Making and Receiving
Phone Calls, Notifying a User, Notifying with Status Bar,
9.2 Vibrating the Phone, Blinking the Lights, Making Noise, Customizing the
Notification, Designing Useful Notification.
For better understanding, you can consider Dialer, Browser, Sim App toolkit, Broadcast
receivers, and so on.
To access the Telephony Manager in your programming, you can simply import the
android.telphony.TelephonyManager class.
After that, you need to use the getSystemService() to create the TelephonyManager
object, and then you can use it to get system information.
Android TelephonyManager
Example
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.telephonyutilites"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TelephonyUtilites"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Activitymain.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="150dp"
android:fontFamily="adamina"
android:text="Mobile Details:"
android:textSize="20dp" />
</RelativeLayout>
ActivityMain.java
package com.example.telephonyutilites;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.TextView;
String PhoneType = ""; // it'll hold the type of phone i.e CDMA / GSM/ None
int phoneType = tele_man.getPhoneType();
switch (phoneType) {
case (TelephonyManager.PHONE_TYPE_CDMA):
PhoneType = "CDMA";
break;
case (TelephonyManager.PHONE_TYPE_GSM):
PhoneType = "GSM";
break;
case (TelephonyManager.PHONE_TYPE_NONE):
PhoneType = "NONE";
break;
}
// true or false for roaming or not
boolean checkRoaming = tele_man.isNetworkRoaming();
String data = "Your Mobile Details are enlisted below: \n";
data += "\n Network Country ISO is - " + nwcountryISO;
data += "\n SIM Country ISO is - " + SIMCountryISO;
data += "\n Network type is - " + PhoneType;
data += "\n Roaming on is - " + checkRoaming;
Function Description
sendTextMessage(String destinationAddress,
sendTextMessage() String scAddress, String text, PendingIntent sentIntent,
PendingIntent deliveryIntent, long messageId)
sendDataMessage(String destinationAddress,
sendDataMessage() String scAddress, short destinationPort, byte[] data,
PendingIntent sentIntent, PendingIntent deliveryIntent)
sendMultimediaMessage(Context context,
sendMultimediaMessage() Uri contentUri, String locationUrl,
Bundle configOverrides, PendingIntent sentIntent
You can create a 2 emulator and Send SMS,Receive SMS 1st emulator to 2nd emulator
calling no-5554 and 5556
Activitymain.xml
package com.example.sendsms;
import androidx.appcompat.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;
</manifest>
Mainactivity,java
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter number"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter message"
android:inputType="textPersonName" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp"
android:text="SEND" />
</LinearLayout>
This chapter lists down all the simple steps to create an application which can be used
to make a Phone Call. You can use Android Intent to make phone call by calling built-in
Phone Call functionality of the Android. Following section explains different parts of our
Intent object required to make a call.
Intent Object - Action to make Phone Call
You will use ACTION_CALL action to trigger built-in phone call functionality
available in Android device. Following is simple syntax to create an intent with
ACTION_CALL action
You can use ACTION_DIAL action instead of ACTION_CALL, in that case you
will have option to modify hardcoded phone number before making a call instead of making
a direct call.
To make a phone call at a given number 91-000-000-0000, you need to specify tel: as
URI using setData() method as follows −
phoneIntent.setData(Uri.parse("tel:91-000-000-0000"));
The interesting point is that, to make a phone call, you do not need to specify any extra data
or data type.
You can create a 2 emulator and calling 1st emulator to 2nd emulator calling no-5554
and 5556.
Example
MainActivity.java
package com.example.callingapp;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
String phone_number = edittext.getText().toString();
callIntent.setData(Uri.parse("tel:" + phone_number));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
});
}
}
activity_main.xml
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="Enter Number"
android:layout_marginTop="98dp" />
<Button
android:id="@+id/buttonCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="164dp"
android:layout_marginBottom="505dp"
android:text="Call " />
</RelativeLayout>
Activitymanifest.xml
<uses-permission android:name="android.permission.CALL_PHONE">
</uses-permission>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CallingApp"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ANDROID NOTIFICATION"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.091"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginBottom="112dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Notify"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activitymain.java
package com.example.notification;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.NotificationChannel;
import android.os.Build;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel("My Notification","My
Notification",NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addNotification();
}
});
}
// Add as notification
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
}
Vibrating the Phone
In android, using vibrate service, we can vibrate android mobile. This example
demonstrate about how to make an Android device vibrate
Mainactivity.java
package com.example.vibratingphone;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
</manifest>
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent"
android:gravity="center"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/text"
android:textSize="18sp"
android:text="Click here to vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Activitymain.xml
</LinearLayout>
MainActivity.java
package com.example.flashlight;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.annotation.RequiresApi;
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleFlashLightOnOff;
private CameraManager cameraManager;
private String getCameraID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleFlashLightOnOff = findViewById(R.id.toggle_flashlight);
cameraManager = (CameraManager)
getSystemService(Context.CAMERA_SERVICE);
try {
getCameraID = cameraManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
public void toggleFlashLight(View view) {
if (toggleFlashLightOnOff.isChecked()) {
try {
cameraManager.setTorchMode(getCameraID, true);
Toast.makeText(MainActivity.this, "Flashlight is turned ON",
Toast.LENGTH_SHORT).show();
} catch (CameraAccessException e) {
e.printStackTrace();
}
} else {
try {
cameraManager.setTorchMode(getCameraID, false);
Toast.makeText(MainActivity.this, "Flashlight is turned OFF",
Toast.LENGTH_SHORT).show();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void finish() {
super.finish();
try {
cameraManager.setTorchMode(getCameraID, false);
Toast.makeText(MainActivity.this, "Flashlight is turned OFF",
Toast.LENGTH_SHORT).show();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
Androidmanifest.xml
</manifest>
Making Noise
Example
Activity_main.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:text="Audio Controller" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="48dp"
android:text="start" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="pause" />
</RelativeLayout>
MainActivity.java
package com.example.mediaplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.mediaplayer">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MediaPlayer"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>