i am trying to disconnect incoming call but facing this error
public interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
private void disconnectPhoneItelephony(Context context)
{
ITelephony telephonyService;
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class<?> c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
}
catch (Exception e)
{
e.printStackTrace();
Log.d("error", e.toString());
}
}
error:
(java.lang.ClassCastException:com.android.internal.telephony.ITelephony$Stub$
Proxy cannot be cast to belllab.com.meetingmanager.ITelephony )
If you have added ITelephony.AIDL file in your project? If not download from here and add it to your project.
You have to add the file like this
Also then your package name must be com/android/internal/telephony/ITelephony.AIDL:
Check this tutorial to implement Aidl file
Related
I would like to be able to close the phone call when it rings but from what I read ITELEPHONY is only valid for API level <28.
Is there another way I can block the phone call?
Below is my code and I get
Accessing hidden method Landroid/telephony/TelephonyManager;->getITelephony()Lcom/android/internal/telephony/ITelephony; (greylist-max-p, reflection, denied) 2021-04-28 16:55:41.884 18265-18265/com.example.myapplication W/System.err: java.lang.NoSuchMethodException: android.telephony.TelephonyManager.getITelephony []
I looked for other solutions already posted on stackoverflow but none worked. In the manifest I have added the following permissions: ANSWER_PHONE_CALLS, READ_PHONE_STATE, CALL_PHONE, READ_CALL_LOG.
I forgot to mention that I also have the ITelephony interface with the specific package com.android.internal.telephony;
public class MyPhoneReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony;
telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
MyPhoneStateListener listener = new MyPhoneStateListener(context);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public static class MyPhoneStateListener extends PhoneStateListener {
Context context;
public MyPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String callingNumber){
super.onCallStateChanged(state, callingNumber);
if((state == TelephonyManager.CALL_STATE_OFFHOOK) || (state == TelephonyManager.CALL_STATE_RINGING))
{
Log.d("CALLING",callingNumber);
BlockTheCall(callingNumber);
}
}
private void BlockTheCall(String callingNumber)
{
Log.d("CALLING",callingNumber);
try{
TelephonyManager tm;
tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
I have this following BroadcastReceiver class to block incoming calls.
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
String info = "Detect Calls sample application\nOutgoing number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
telephonyService.endCall();
} catch (Exception e) {
System.out.println("error on end call : "+e.getMessage());
e.printStackTrace();
}
}
}
Everything is working as intended but my issue is, everytime there is a delay in telephonyService.endCall();. Meaning, I can hear a fraction of ringtone before it gets disconnected. Also, the call disconnected by endCall() is not counted as a missed call but as a incoming call. Is this the default behaviour?
Thanks for any inputs.
I'm creating an app in which a user can choose any number to block
I think right algorithm is : when phone rings, a function check that number is in list or not (check in database) and if there is, reject that call
i found this code in internet but it doesn't work,should i call onReceive function in my activity? what should i wrote on main activity?
here is the code:
public class IncomingCallReceiver extends BroadcastReceiver {
private ITelephony telephonyService;
private String blacklistednumber = "+458664455";
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony) m.invoke(tm);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.e("INCOMING", phoneNumber);
if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) {
telephonyService.silenceRinger();
telephonyService.endCall();
Log.e("HANG UP", phoneNumber);
}
} catch (Exception e) {
e.printStackTrace();
}
}
interface:
public interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
i also added this below code to manifest
<receiver android:name="IncomingCallReceiver" >
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permissionandroid:name="android.permission.PROCESS_INCOMING_CALLS"/>
did i miss anything?
I want to disconnect incoming call programmatically, I searched regarding this, I found so many links on stack overflow. I tried all of them, but none of them worked
I created ITelephony class in package com.android.internal.telephony
This is the code in ITelephony class
The code I used is...
public class Incommingcall extends BroadcastReceiver{
TelephonyManager manager;
int callstate;
private ITelephony telephonyService;
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try{
manager = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
callstate = manager.getCallState();
if(callstate == TelephonyManager.CALL_STATE_RINGING){
Log.i("IncommingCall","Call is Ringing");
Class c = Class.forName(manager.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
Log.i("m= ", ""+m);
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(manager);
telephonyService.endCall();
Log.i("IncommingCall","Call Disconnected");
}else if (callstate == TelephonyManager.CALL_STATE_IDLE) {
}else if(callstate == TelephonyManager.CALL_STATE_OFFHOOK){
}
}catch(Exception e){
}
}
}
In the manifest file
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
The Logcat I got is...
08-04 13:57:48.686: I/IncommingCall(29302): Call is Ringing
08-04 13:57:48.686: I/m=(29302): private com.android.internal.telephony.ITelephony android.telephony.TelephonyManager.getITelephony()
You have to use PhonestateListener plese check below way
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// Disconnect the call here...
try {
TelephonyManager manager = (TelephonyManager) mContext
.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(manager.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephony = (ITelephony) m.invoke(manager);
telephony.endCall();
} catch (Exception e) {
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
}
start lisener in reciever as follows
TelephonyManager tmgr;
MyPhoneStateListener PhoneListener = new MyPhoneStateListener()
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
I added these two permissions and now its working fine
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />
In my app i'm using auto end of call for a list of numbers in a such way...
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = null;
String incommingNumber;
String inc= "+799999999";
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
try {
// Java reflection to gain access to TelephonyManager's
// ITelephony getter
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) m.invoke(tm);
Bundle b = intent.getExtras();
incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v(TAG,incommingNumber );
Log.v(TAG,incno1 );
if ( incommingNumber.equals(inc) )
{
telephonyService = (ITelephony) m.invoke(tm);
telephonyService.silenceRinger();
telephonyService.endCall();
Log.v(TAG,"BYE BYE BYE" );
}
else{
telephonyService.answerRingingCall();
Log.v(TAG,"HELLO HELLO HELLO" );
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
}
}
It works for me... But missed call is shown in the phone... Is it possible to hide it and not to display?
Yes, use the following line to Cancel the call notification.
telephonyService.cancelMissedCallsNotification();