BroadcastReceiver para cuando se hace una llamada
(Outgoing.java) - requiere PROCESS_OUTGOING_CALLS
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Outgoing extends BroadcastReceiver {
public Outgoing() {
}
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context,
"Outgoing: " + number,
Toast.LENGTH_LONG).show();
}
}
Broadcast Recevier para los estados de la llamada
(PhoneState.java) - Requiere READ_PHONE_STATE
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneState extends BroadcastReceiver {
String TAG = "com.desarrolladoresandroid.estadollamadasaliente";
public PhoneState() {
}
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.contains(TelephonyManager.EXTRA_STATE_IDLE)){
Log.d(TAG, "EXTRA_STATE_IDLE");
showToast("EXTRA_STATE_IDLE", context);
}else if(state.contains(TelephonyManager.EXTRA_STATE_RINGING)){
Log.d(TAG, "EXTRA_STATE_RINGING");
showToast("EXTRA_STATE_RINGING", context);
}else if(state.contains(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Log.d(TAG, "EXTRA_STATE_OFFHOOK");
showToast("EXTRA_STATE_OFFHOOK", context);
}
}
private void showToast(String msg, Context ctx){
Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
}
} //
Declaración de los B. Receiver's en manifest (habrá que revisar los atributos enabled y exported que figuran así por defecto, dado que estoy copiando código de ejemplo desde el ide)
<receiver
android:name=".PhoneState"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999" >
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name=".Outgoing"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="999">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>