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();
Related
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
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?
with statrtActivity(callIntent), call goes and then i have to wait for few seconds and end automatically. to end up my call i have taken mycalss extends Broadcastreceiver then in that onreceive() i implemented.in that method i got only to set old number and newnumber and toast is printing.
What i want exactly is to end call what i need to write. and how to call onreceive method from my class? plase help me. i didnt get anywhere.
#Override
public void onReceive(Context context, Intent intent) {
final String oldNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
this.setResultData(newPhNnumber);
final String newNumber = this.getResultData();
if((newNumber!=null)&&(newNumber!=oldNumber))
{
String msg = "Intercepted outgoing call. Old number " + oldNumber + ", new number " + newNumber;
Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
this.abortBroadcast();----> what it does?
}
You may try this:
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);
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
}
you simply abort outgoing call through following code
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
phoneNo = intent
.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if (phoneNo.isBlocked(blockNo)) {
setResultData(null);
}
}
}
my VoiP-client would like to block all incoming calls to the phone while my own call is taking place. Is it possible to do this somehow?
Yes you can block incoming calls. Using this code
String phonestate = bundle.getString(TelephonyManager.EXTRA_STATE);
try
{
if (TelephonyManager.EXTRA_STATE_RINGING.equalsIgnoreCase(phonestate))
{
try
{
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
}
catch (Exception e)
{
e.printStackTrace();
}
if (prefStatus != null)
{
if (count == 1)
{
telephonyService.endCall();
String prefLoginemail = myloginpref.getString("emailid", null);
System.out.println(printdata + " prefLoginemail :: "+ prefLoginemail);
Global.mNotificationManager = (NotificationManager) mxcontext.getSystemService(Context.NOTIFICATION_SERVICE);
notifyDetails = new Notification(com.CellPhoneTerror.R.drawable.icon1,"CellPhoneTerror!", System.currentTimeMillis());
myIntent = PendingIntent.getActivity(mxcontext, 0, new Intent(mxcontext, clsHomePage.class), 0);
notifyDetails.setLatestEventInfo(mxcontext,"You Got a Call from Blocked Number.", "",myIntent);
notifyDetails.flags |= Notification.FLAG_AUTO_CANCEL;
Global.mNotificationManager.notify(Global.SIMPLE_NOTFICATION_ID,notifyDetails);
}
}
}
if (prefIncomingBlock.equals("true"))
{
if (TelephonyManager.EXTRA_STATE_IDLE.equalsIgnoreCase(phonestate))
{
if (count == 0)
{
System.out.println("if Cellphoneterror:"+ Global.clsIncomingNum);
Intent block = new Intent(context, dialogbox1.class);
block.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(block);
}
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
Thanks.
Not with the current SDK no, You can monitor incoming calls and give the user the option to answer with your application instead of the native app