После долгого времени поиска и логики, чтобы решить эту проблему, я получаю решение:
Задача : Если я позвоню человеку, если мой звонок не будет принятчеловеком, с моей стороны я должен показать тост, если мой вызов отклонен
Решение: Создать получателя, который регистрируется двумя действиями
android.intent.action.NEW_OUTGOING_CALL android.intent.action.PHONE_STATE
Оба действия необходимы.
Для этого вам необходимо получить разрешение
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Manifast.xml
<receiver android:name="Call_Receiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
И ваш класс BroadcastReceiver
* Call_Receiver.java *
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
*
*/
public class Call_Receiver extends BroadcastReceiver {
private Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
ctx = context;
OutgoingIncomingCallListener phoneListener = new OutgoingIncomingCallListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
Log.i("", " :::::::::::: inside onReceive if &&&&&& :::::::::::::");
telephony.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class OutgoingIncomingCallListener extends PhoneStateListener {
public boolean wasRinging;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.i("", " ************ RINGING ********");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("", " *********** OFFHOOK ********");
if (!wasRinging) {
// Start your new activity
Log.i("", " *********** if ********");
} else {
// Cancel your old activity
Log.i("", " *********** else ********");
}
// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i("", " ::::::::::: IDLE :::::::::");
// this should be the last piece of code before the break
// Log.i("", " *********** OFFHOOK ********");
if (wasRinging) {
Toast.makeText(ctx,
"Your call is disconnected by receiver", 10000)
.show();
}
wasRinging = true;
break;
}
}
}
}
Это покажет вамтост при звонке не принимается.