Android : Detecting Incoming and Outgoing phone calls on Mobile
Handling Incoming calls:
To handle Incoming calls, we need to use TelephonyManager class, and it's implemented method "listen". To register a listener, that will receive call state, data connection, network, sim and other events, related to telephony.1.This requires android.permission.READ_PHONE_STATE permission.
Create our listener class, derived from PhoneStateListener, and override onCallStateChanged method, as follows:The states are - CALL_STATE_IDLE, CALL_STATE_OFFHOOK, and CALL_STATE_RINGING
IDLE - no incoming call
OFFHOOK –the line is busy
RINGING – call is incoming
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.Toast;
public class KTPotActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
Handling Outgoing call:
1. Create an OutgoingBroadcastReceiver
public class OutgoingReceiver extends BroadcastReceiver {@Override
public void onReceive(Context context, Intent intent) {
Log.d("call reciever", intent.toString());
Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
}
2. Register OutgoingCallBroadcastReceiver in AndroidManifest.xml
<receiver android:name="OutgoingReceiver" ><intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
3. Add permission in AndroidManifest.xml
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>4.Detect outgoing call:
@Overridepublic void onReceive(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.d("outgoing call", intent.toString() + ", call to: " + phoneNumber);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
0 comments:
Post a Comment