I use this code to block a incomming Calls in android Application..
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.silenceRinger();
telephonyService.endCall();
This Code is not work Properly.. At least one time ringging a phone and after cut calls..
Please.. Some one Give me better solution..
Thanks to u. Problem is that at least one time ringging device and after cut calls so not cut calls but missed calls..
Related
I have a method function that is activated when a call is coming and I hit a Button on my screen app.
Below You can see my code:
public void answerCall() throws Exception {
// answerPhoneHeadsethook(mContext);
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
#SuppressWarnings("rawtypes")
Class c = Class.forName(tm.getClass().getName());
#SuppressWarnings("unchecked")
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
// telephonyService.answerRingingCall();
answerPhoneHeadsethookd(mContext);
answerPhoneAidl(mContext);
}
private void answerPhoneAidl(Context context) throws Exception {
// Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService;
telephonyService = (ITelephony)m.invoke(tm);
// Silence the ringer and answer the call!
telephonyService.silenceRinger();
telephonyService.answerRingingCall();
}
private void answerPhoneHeadsethookd(Context context) {
// Simulate a press of the headset button to pick up the call
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");
// froyo and beyond trigger on buttonUp instead of buttonDown
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
}
Unfortunately this function is not working.
Google doesn't want you to do that, and it makes sense, could be abused in so many ways...
If you want to try some "illegal" ways, have a look here:
How to programmatically answer/end a call in Android 4.1?
I making a phone call cancel app. Basically it cancel a upcoming call if your phone is in back position in table or ground (Accelerometer Data). I make a Broadcast Receiver and entry it on the manifest also gave action this android.intent.action.PHONE_STATE
public class PhoneCallReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent si=new Intent(context, MyService.class);
context.startService(si);
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
//telephonyService.silenceRinger();
telephonyService.endCall();
Log.e("in try catch", "yes");
Log.e("in try catch", "call cancel");
shrededit.putInt("newcallingstate", 0);
shrededit.commit();
context.stopService(si);
} catch (Exception e) {
e.printStackTrace();
}
Log.e("pr", "out side true block");
}
}
My code is running very well if i only use this code without the accelerometer service class but when i use accelerometer class and make a intent before this below code. My app not canceling the call or not giving any type or error. I think it but not completely sure it is context problem.
So please help me.
No,its not context problem all this code is perfectly fine you does just need a condition or use sharedpreferences for condition.
first make a sharedpreferences object in your service class or that which you show in intent. like this
SharedPreferences sharedpref=this.getSharedPreferences("your_file_name",Context.MODE_PRIVATE);
SharedPreferences.Editor sharededit=sharedpref.edit();
sharededit.putBoolean("String_name", true);
sharededit.commit();
Here boolean is not important you can choose and other data type it depends you.
Then in you Broadcast receiver class you have to again use these SharedPreference and SharedPreference.Editor function and you get the previous boolean value by this method
boolean value=sharededit.getBoolean("callingStop", true);
Than make a condition like this
if(value==true){TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
//telephonyService.silenceRinger();
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
} }
This is all now your code is run without any error.
I'm having problem in phone state listener. I want to call one activity from onCallStateChanged event. When i get an incoming call i want to call one activity and process the transaction of the current phone no(which i currently get by incoming call).
But i couldn't go to ShowPhoneStateDialogActivity activity using following code.Please correct me what is my mistake. Thanks in advance.
My code is,
case TelephonyManager.CALL_STATE_RINGING:
Log.d("PHONE:", "RINGING");
Log.w("Call STATE:", "RINGING");
if (!sess.getCallActive()) {
sess.setCallActive(true);
sess.setActiveMobileNo(incomingCallNumber);
this.endActivecall();
Intent intent = new Intent(context,ShowPhoneStateDialogActivity.class).setAction("incomingNumber");
intent.putExtra("Phoneno", incomingCallNumber);
Log.i("CURRENT ACTIVITY",this.getClass().getSimpleName());
Log.i("CURRENT CONTEXT","Context:"+context);
//intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intent);
break;
} else {
this.endActivecall();
}
break;
And endActivecall function is,
public void endActivecall() {
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
// telephonyService.silenceRinger();
telephonyService.endCall();
Log.i("CALL STATE ACTION:", "Call end");
Log.i("ACTIVE_MOBILENO:", sess.getActiveMobileNo());
} catch (Exception e) {
e.printStackTrace();
}
Instead of the code context.startService(intent), try calling context.startActivity(intent) instead.
i have used the following code:
Problem: It is working fine and blocking incoming call but phone ring for fraction of a second before disconnect the call. can there be a solution which i am looking for .
public class CallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
incomingNumber1 = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager 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();
}}
I have not found the answer to the problem yet. I want solution which work perfectly on Android 2.3. Incoming call is to be blocked fully there must not any ring.
I am working on application which will use the Telephony API.
I am using:
Context context = getBaseContext();
TelephonyManager tMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
try {
Class<?> c = Class.forName(tMgr.getClass().getName());
Method method = c.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(tMgr);
telephonyService.silenceRinger();
telephonyService.answerRingingCall();
} catch (Exception e) {
// exception handling
}
This code is used to auto answer call, and works fine in Android 2.2, but doesn't work in Android 2.3.
Is there any replacement of Telephony API in Android 2.3?
in your case its only the permission that changed from MODIFY_PHONE_STATE to READ_PHONE_STATE and if you want to accerss if via broadcastreceiver from outside your app, you'll need permission GET_TASK too ....