Reject Call Silently Using Incallservice
i am trying to reject an incoming call using InCallServiceby making my app default dialer app.It is rejecting call fine but the problem is if the mobile is on vibration mode its vi
Solution 1:
If you're making your app the default dialer app, try implementing the CallScreeningService API. That API will allow you to intercept and block calls prior to the system starting ringing.
Solution 2:
I would recommend you to change the approach.
Step 1 : Create a broadcast receiver
publicclassCallReceiverextendsBroadcastReceiver {
private Context context;
@OverridepublicvoidonReceive(Context context, Intent intent) {
this.context = context;
rejectCall();
}
publicvoidrejectCall() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
TelecomManagertelecomManager= (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if (telecomManager != null) {
telecomManager.endCall();
}
} else {
rejectCallViaTelephonyManager();
}
Log.d(TAG, "call ended successfully.");
} catch (Exception e) {
e.printStackTrace();
}
}
privatevoidrejectCallViaTelephonyManager() {
ITelephonytelephonyService= getTelephonyService(context);
if (telephonyService != null) {
telephonyService.silenceRinger();
telephonyService.endCall();
}
}
private ITelephony getTelephonyService(Context context) {
TelephonyManagertm= (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Classc= Class.forName(tm.getClass().getName());
Methodm= c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
return (ITelephony) m.invoke(tm);
} catch (Exception e) {
e.printStackTrace();
}
returnnull;
}
}
Step 2: Add this receiver to your manifest file
<receiverandroid:name=".CallReceiver"android:enabled="true"android:exported="true"><intent-filterandroid:priority="999"><actionandroid:name="android.intent.action.PHONE_STATE" /></intent-filter></receiver>
Step 3: You need to add permission as well
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE" />
Post a Comment for "Reject Call Silently Using Incallservice"