Top Banner
Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar , Gyan Khand - 3, Indirapuram , Ghaziabad Website: www.sisoft.in Email:[email protected] Phone: +91 - 9999 - 283 - 283 1
29

Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Apr 05, 2018

Download

Documents

lykhuong
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Sisoft Technologies Pvt LtdSRC E7, Shipra Riviera Bazar, Gyan Khand-3, Indirapuram, Ghaziabad

Website: www.sisoft.in Email:[email protected]: +91-9999-283-283

1

Page 2: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Android Telephony Manager provides access to the information

about the Telephony Services on the devices.

o Application will be the using the methods in the TelephonyManager class to determine Telephony States, services, subscriber information .

o It consist of the android.telephony and android.telephony.gsmpackages

o Applications will be registering a listener to receive notification of changes in the telephone states

2

Telephony Manager

Page 3: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Telephonic Manager Contents

o State of the Mobile

o Sim Card

o Uniqueness Features

o Devices

o Phone Type

3

Page 4: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Telephony Manager provides access to information about the telephony services on the device. Applications can use the methods in this class to determine telephony services and states, as well as to access some types of subscriber information. Applications can also register a listener to receive notification of telephony state changes.

You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE).

4

Telephony Manager

Page 5: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Permission Required:

To work with Telephony Manager and to read the phone details we need<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

permission. So add this permission in your manifest file.

5

Telephony Manager

Page 6: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Accessing the Telephony Manager :

Have an object of TelephonyMnager

TelephonyManager tm ;

tm= (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

The getDeviceId() method is used to get the IMEI/MEID of the device. If the device is a GSM device then IMEI will be returned and if the device is a CDMA device then MEID will be returned. This device id can be used to uniquely identify the device

6

Telephony Manager

Page 7: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Get IMEI Number of Phone

String IMEINumber=tm.getDeviceId();

o Get Subscriber ID

String subscriberID=tm.getDeviceId();

o Get SIM Serial Number

String SIMSerialNumber=tm.getSimSerialNumber();

o Get Network Country ISO Code

String networkCountryISO=tm.getNetworkCountryIso();

o Get SIM Country ISO Code

String SIMCountryISO=tm.getSimCountryIso();

7

Accessing the Telephony Manager

Page 8: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Get the device software version

String softwareVersion=tm.getDeviceSoftwareVersion()

o Get the Voice mail number

String voiceMailNumber=tm.getVoiceMailNumber();

8

Accessing the Telephony Manager

Page 9: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Get the Phone Type CDMA/GSM/NONE/ /Get the type of network you are connected with

int phoneType=tm.getPhoneType();

switch (phoneType){

case (TelephonyManager.PHONE_TYPE_CDMA):// your code

break;case (TelephonyManager.PHONE_TYPE_GSM)

// your codebreak;

case (TelephonyManager.PHONE_TYPE_NONE):// your code

break;}

9

Accessing the Telephony Manager

Page 10: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Get the SIM state/Details

int SIMState=tm.getSimState();switch(SIMState){

case TelephonyManager.SIM_STATE_ABSENT :// your codebreak;

case TelephonyManager.SIM_STATE_NETWORK_LOCKED :// your codebreak;

case TelephonyManager.SIM_STATE_PIN_REQUIRED :// your codebreak;

case TelephonyManager.SIM_STATE_PUK_REQUIRED :// your codebreak;

case TelephonyManager.SIM_STATE_READY :// your codebreak;

case TelephonyManager.SIM_STATE_UNKNOWN :// your codebreak;

}10

Accessing the Telephony Manager

Page 11: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Getting SIM Details :Using the Object of Telephony Manager class we can get the details like SIM Serial number, Country Code, Network Provider code and other Details.

11

int simState = telephonyManager.getSimState();switch (simState){

case (TelephonyManager.SIM_STATE_ABSENT): break;case (TelephonyManager.SIM_STATE_NETWORK_LOCKED): break;case (TelephonyManager.SIM_STATE_PIN_REQUIRED): break;case (TelephonyManager.SIM_STATE_PUK_REQUIRED): break;case (TelephonyManager.SIM_STATE_UNKNOWN): break;case (TelephonyManager.SIM_STATE_READY):{

// Get the SIM country ISO codeString simCountry = telephonyManager.getSimCountryIso();// Get the operator code of the active SIM (MCC + MNC)

String simOperatorCode = telephonyManager.getSimOperator();// Get the name of the SIM operatorString simOperatorName = telephonyManager.getSimOperatorName();// -- Requires READ_PHONE_STATE uses-permission --// Get the SIM’s serial numberString simSerial = telephonyManager.getSimSerialNumber();

}}

Accessing the Telephony Manager

Page 12: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Getting Network Details

// Get connected network country ISO codeString networkCountry = telephonyManager.getNetworkCountryIso();

// Get the connected network operator ID (MCC + MNC)String networkOperatorId = telephonyManager.getNetworkOperator();

// Get the connected network operator nameString networkName = telephonyManager.getNetworkOperatorName();

// Get the type of network you are connected withint networkType = telephonyManager.getNetworkType();switch (networkType){case (TelephonyManager.NETWORK_TYPE_1xRTT) :" Your Code ":break;case (TelephonyManager.NETWORK_TYPE_CDMA) :" Your Code ":break;case (TelephonyManager.NETWORK_TYPE_EDGE) : " Your Code ":break;case (TelephonyManager.NETWORK_TYPE_EVDO_0) :" Your Code ":break;

12

Accessing the Telephony Manager

Page 13: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

o Find whether the Phone is in Roaming, returns true if in roaming

boolean isRoaming=tm.isNetworkRoaming();

if(isRoaming)phoneDetails+="\nIs In Roaming :"+"YES";

elsephoneDetails+="\nIs In Roaming : "+"NO";

13

Accessing the Telephony Manager

Page 14: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Calls Messaging

Outgoing CallMessage

Incoming Call Message Sending

Message

ReceivingMessage

14

Telephony Manager

Page 15: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Incoming Call Message

Monitoring Calls

Managing Calls

Initializing calls

15

Telephony Manager

Page 16: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Incoming Call Receiver In Android

o Broadcast Receiver is better way than Activity to listen for Incoming Calls.

o So declare the Permission and Receiver in Manifest

Permission Required:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

16

Page 17: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.incomingcallreciever"android:versionCode="1"android:versionName="1.0" >

<uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" >

<receiver android:name=".IncommingCallReceiver" android:enabled="true"><intent-filter>

<action android:name="android.intent.action.PHONE_STATE" />

</intent-filter></receiver>

</application>

</manifest> 17

Declare the receiver and register it to listen "android.intent.action.PHONE_STATE“ action android.manifest

Incoming Call Receiver In Android

Page 18: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

public class IncommingCallReceiver extends BroadcastReceiver {Context mContext;

@Overridepublic void onReceive(Context mContext, Intent intent) {

try{

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){

Toast.makeText(mContext, "Phone Is Ringing", Toast.LENGTH_LONG).show();// Your Code

}if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){

Toast.makeText(mContext, "Call Recieved", Toast.LENGTH_LONG).show();// Your Code

}if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){

Toast.makeText(mContext, "Phone Is Idle", Toast.LENGTH_LONG).show();// Your Code

} }catch(Exception e) { //your custom message }

} } 18

Incoming Call Receiver :-

Incoming Call Receiver In Android

Page 19: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Running Screens :-

19

Incoming Call Receiver In Android

Page 20: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

SMS Manager

20

Page 21: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

SMS Manager

Manages SMS operations such as sending data, text, and pdu SMS messages. Get this object by calling the static method SmsManager.getDefault().

java.lang.Object

↳android.telephony.gsm.SmsManager

This class was deprecated in API level 4.Replaced by android.telephony.SmsManager that supports both GSM and CDMA.

21

Page 22: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

SMS ManagerA very useful feature in Android is the SmsManager API which you can use to send an SMS from inside your own Application. Actually there are two way you can send an SMS from your Application:

Using SmsManager API Using the build in SMS activity of Android API

o Using the built in Application is fairly easy, because you don’t have to do anything special really. You just have to call the built in Activity.

o But using the SmsManagerAPI, you can customize your own Activity the way you want and use it in any manner you choose, so it’s much more flexible. So, in this tutorial we are going to examine both ways.

22

Page 23: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

1. Using the build in SMS activity of Android API

Using the built in Application is fairly easy, because you don’t have to do anything special really. You just have to call the built in Activity.

23

Intent smsIntent = new Intent(Intent.ACTION_VIEW);smsIntent.putExtra("sms_body", “Hello sisoft");smsIntent.setType("vnd.android-dir/mms-sms");startActivity(smsIntent);

Page 24: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

2.Using SmsManager API

• But using the SmsManagerAPI, you can customize your own Activity the way you want and use it in any manner you choose, so it’s much more flexible. So, in this tutorial we are going to examine both ways.

24

SmsManager smsManager = SmsManager.getDefault();smsManager.sendTextMessage(phoneNo, null, sms, null, null);

Page 25: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Example 1: Send an SMS using the SmsManager APICreate the main Layout of the Application : Open res/layout/main.xml file :

25

Page 26: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

• And paste the following code in order to set the appropriate SMS permission for the Application

26

Example 1: Send an SMS using the SmsManager API

Page 27: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Go to the java file that contains the code of the activity you’ve just created

27

Example 1: Send an SMS using the SmsManager API

Page 28: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Example 2: Send an SMS using the Built-in SMS application

1. Create the main Layout of the Application

Open res/layout/main.xml file :

28

Page 29: Sisoft Technologies Pvt Ltd SRC E7, Shipra Riviera Bazar ...sisoft.in/references/android/ppt18_Telephony Manager_D3.pdf · public class IncommingCallReceiver extends BroadcastReceiver

Example 2: Send an SMS using the Built-in SMS application• Go to the java file that contains the code of the activity you’ve just created:

29