image/svg+xml $ $ ing$ ing$ ces$ ces$ Res Res ea ea Res->ea ou ou Res->ou r r ea->r ch ch ea->ch r->ces$ r->ch ch->$ ch->ing$ T T T->ea ou->r

Cellular telephony

Pile de téléphonie

Dialing a call

First we must declare the required permissions in the manifest :
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

// We create the listener
PhoneCallListener phoneListener = new PhoneCallListener() {
	private boolean calling = false ;
	@Override public void onCallStateChanged(int state, String incomingNumber) {
		switch (state)
		{
			case TelephonyManager.CALL_STATE_RINGING :
				// Phone is ringing
				break ;
			case TelephonyManager.CALL_STATE_OFFHOOK :
				// Phone call is starting
				calling = true ;
				break ;
			case TelephonyManager.CALL_STATE_IDLE :
				// Either the phone has not dialed yet (!calling), or the call is finished (calling == true)
				break ;
		}
}
// We register the listener
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

Information about the cellular network with TelephonyManager

PhoneStateListener

Incoming calls

Action possible :

Managing text messages

Sending SMS

An activity to send SMS

Content not available

SMS receival

BroadcastReceiver to receive SMS

package fr.upem.coursand.sms;

import java.util.regex.Pattern;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

/**
 * Intercept SMSs containing a given regular expression.
 */
public class SMSInterceptor extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		Log.v("SMSInterceptor", "Receiving intent " + intent);
		Object[] pdus = (Object[])intent.getExtras().get("pdus");
		for (Object pdu: pdus)
		{
			SmsMessage message = SmsMessage.createFromPdu((byte[])pdu);
			Log.v("SMSInterceptor", "Content of the message: " + message.getDisplayMessageBody());
			if (isDiscardable(message))
			{
				Log.v("SMSInterceptor", "Discarding message " + message); // The message is not propagated: it is lost
				abortBroadcast(); // this SMS is not propagated on the chain of receivers (the SMS app should not receive it)
			} else
			{
				// communicate the intercepted message to the SMSSpyService
				Intent i = new Intent(context, SMSSpyService.class).setAction(SMSSpyService.GIVE_MESSAGE_ACTION);
				i.putExtra("message", message.getDisplayMessageBody());
				i.putExtra("sender", message.getOriginatingAddress());
				context.startService(i);
			}
		}
	}
	
	public static Pattern DISCARD_PATTERN = Pattern.compile("^destroy:.*$");

	public boolean isDiscardable(SmsMessage message)
	{
		return DISCARD_PATTERN.matcher(message.getDisplayMessageBody()).matches();
	}
}

We declare the BroadcastReceiver in the manifest to instantiate it automatically when a new SMS arrives:

<receiver android:name="fr.upem.coursand.sms.SMSInterceptor">
	<intent-filter android:priority="1000">
		<action android:name="android.provider.Telephony.SMS_RECEIVED" />
	</intent-filter>
</receiver>