I want to send SMS after reject incoming call.
App is sending the SMS but the problem is that it is sending it twice. I am unable to figure out where problem is.
Below is the code used.
public class CallBarring extends BroadcastReceiver
{
private String number;
#Override
public void onReceive(Context context, Intent intent)
{
// If, the received action is not a type of "Phone_State", ignore it
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
// Else, try to do some action
else
{
// Fetch the number of incoming call
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// Check, whether this is a member of "Black listed" phone numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
}
}
public void sendSMS(Context context) {
try{
String message = SharedPrefActivity.CommonMethod.getPrefsData(context, SharedPrefActivity.Constants.TextMessage, "");
Intent intent=new Intent(context,CallBarring.class);
PendingIntent pi=PendingIntent.getActivity(context, 0, intent,0);
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(number, null, message, pi,null);
Toast.makeText(context, "SMS Sent", Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(context, "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
// Method to disconnect phone automatically and programmatically
// Keep this method as it is
#SuppressWarnings({ "rawtypes", "unchecked" })
private void disconnectPhoneItelephony(Context context)
{
ITelephony telephonyService;
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.endCall();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Try This:
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context);
// return;
}else{
disconnectPhoneItelephony(context);
sendSMS(context);
}
This to
// Check, whether this is a. member of "Black listed" phone. numbers stored in the database
if(MainActivity.blockList.contains(new Blacklist(number)))
{
// If yes, invoke the method
disconnectPhoneItelephony(context);
sendSMS(context)
}
I have done the call recording in my application and am calling one Activity when the phone is in Ringing state.In that Activity just am showing the phone number and person name. For that I have written the code in my class which it extends BroadcastReceiver.
This scenario is working fine when I use the application. But If I close the application then am getting Unfortunately stopped
This is my AndroidManifest.xml.
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
This is my onRecive method. When am using my application If i get a call then the current activity is not going to the background. So I have written the code to finish the Activity.
#Override
public void onReceive(Context context, Intent intent) {
LoginActivity.login.finish();
MainActivity.main.finish();
telephonyRegister(context,intent);
}
This is the functionality. I'am calling the ProjectDailogActivity when the phone state is Ringing. I'am finishing the same Activity when the call is attended or ended.
public void telephonyRegister(final Context context, Intent intent)
{
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
ctx=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(ring==true&&callReceived==false)
{
Toast.makeText(context, "THIS IS MISSED CALL FROM"+phoneNumber, Toast.LENGTH_LONG).show();
String smsmessage = "Thanks for contacting us. We will get back you soon";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, "Dear Customer!"+" "+smsmessage, null, null);
Log.i("sms",smsmessage);
Toast.makeText(context, "SMS sent.",
Toast.LENGTH_LONG).show();
callReceived=false;
}
else if(callReceived==true)
{
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
if(Conntact.isAvailable(phoneNumber))
{
try{
myAudioRecorder.release();
myAudioRecorder.stop();
myAudioRecorder = null;
}
catch(Exception e)
{
System.out.print("error");
}
Toast.makeText(context,"Recording Stopped", Toast.LENGTH_SHORT).show();
}
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK))
{
try{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
dir = Environment.getExternalStorageDirectory();
try {
file = File.createTempFile("sound", ".mp3", dir);
} catch (IOException e) {
e.printStackTrace();
return;
}
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(file.getAbsolutePath());
if(Conntact.isAvailable(phoneNumber))
{
callReceived = true;
ring=false;
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(context, "recording started", Toast.LENGTH_SHORT).show();
}
else
{
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)){
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
ring = true;
System.out.println("phonenum"+Conntact.isAvailable(phoneNumber));
if(!Conntact.isAvailable(phoneNumber))
{
Toast.makeText(ctx, phoneNumber+"not available", Toast.LENGTH_SHORT).show();
}
else if(Conntact.isAvailable(phoneNumber))
{
Intent intent11 = new Intent(ctx,ProjectDailogActivity.class);
intent11.putExtra("incoming_number",phoneNumber);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent11);
}
}
}
Conntact is a table where already some numbers has been stored. So when I get a call just am checking whether the calling number is available in the Conntact table.
If it is available then show the ProjectDailogActivity. If the number is available and the call is also picked means then do the recording. Everything is working fine . But it shows Unfortunately stopped when my app is not in use.
Consider unregistering for broadcast events while quitting from your app,Click here for a SO answer on same issue
Hi i am blinking the flashlight on incoming call and i am able to do that using this code but the problem is the flashlight is not getting stopped when the call is attended or cut
I am trying to stop the service from every way but the flashlight doesnot stops untill the loop is completed
In receiver class...
Intent in = new Intent(context, Run.class);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
in.putExtra("state", state);
context.startService(in);
} else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
// Toast.makeText(context, "Idle", Toast.LENGTH_LONG).show();
context.stopService(in);
} else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
// Toast.makeText(context, "Offhook", Toast.LENGTH_LONG).show();
context.stopService(in);
}
In Service class...
Camera cam;
Parameters p;
String state;
String tag="Runserve";
#Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
try {
cam = Camera.open();
p = cam.getParameters();
String myString = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101011";
long blinkDelay = 50;
for (int i = 0; i < myString.length(); i++) {
state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
break;
}else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
break;
}
if (myString.charAt(i) == '0') {
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
} else {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(p);
}
Thread.sleep(blinkDelay);
}
}catch (Exception e) {
// TODO: handle exception
Log.d(tag, "in catch1");
Log.d(tag, e.toString());
}
}else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
try {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.release();
} catch (Exception e) {
// TODO: handle exception
Log.d(tag, e.toString());
}
stopSelf();
}else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
try {
p.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.release();
} catch (Exception e) {
// TODO: handle exception
Log.d(tag, e.toString());
}
stopSelf();
}
}
I am making the app in which i can attend the call through proximity sensor.This code has 2 intents First is i i.e working for versions less than Android 4.0 and another is headSetUnPluggedintent i.e working on Android 4.1. Is there any single way through which calls can be attended by all versions
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
TelephonyManager mgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
int callState = mgr.getCallState();
if(callState==TelephonyManager.CALL_STATE_RINGING) {
callState = mgr.getCallState();
String x="0.0";
String y=(String.valueOf(event.values[0]));
if( x.equals(y)){
//Toast.makeText(getApplicationContext(), "Proxy", Toast.LENGTH_SHORT).show();
try{
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(i, "android.permission.CALL_PRIVILEGED");
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0);
headSetUnPluggedintent.putExtra("name", "Headset");
sendOrderedBroadcast(headSetUnPluggedintent, null);
if(callState==TelephonyManager.CALL_STATE_OFFHOOK){
headSetUnPluggedintent=null;
i=null;
}else if(callState==TelephonyManager.CALL_STATE_IDLE){
headSetUnPluggedintent=null;
i=null;
}
}catch(Exception e){
Log.d(tag, e.toString());
}
}
Several points here :
in the for loop, it's useless to check the value of state as you have already checked it in your if condition. state is a String (i.e.immutable), it's value is not going to change.
you need to get, for each iteration, the current state of the call, using TelephonyManager#getCallState(). Something like this (not tested) :
TelephonyManager mgr = getSystemService(Context.TELEPHONY_SERVICE);
int callState = mgr.getCallState();
if (callState != TelephonyManager.CALL_STATE_RINGING )
break;
You should normally not attempt to stop a IntentService, it stops itself when all work is done (that is when onHandleIntent returns).
Also, the IntentService javadoc states that you should not call stopSelf().
I am writing an android app in which I need to answer an incoming call, do some work and then end the call.
After all the Googling I could find two different ways to achieve this both of which do not work with recent versions of Android, specifically after 4.1, Jelly Bean.
I.) Access "com.android.internal.telephony.ITelephony" using Java Reflection in the Broadcast receiver for "android.intent.action.PHONE_STATE". Below sample code can be found in hundreds of related post:
public class PhoneCallReceiver extends BroadcastReceiver {
Context context = null;
private static final String TAG = "Phone call";
private ITelephony telephonyService;
#Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
Log.v(TAG, "Receving....");
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
Log.v(TAG, "Answering Call now...");
telephonyService.answerRingingCall();
Log.v(TAG, "Call answered...");
//telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
}
}
The problem with this code is that
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
is required for this method to work, and this permission has been defined as "for system apps only" from android v 2.3. In short, normal user apps can not define this permission in the manifest file anymore.
II.) Another way is to simulate pushing of the Headset hook which makes Android answer the call. This is done by broadcasting the "Intent.ACTION_MEDIA_BUTTON" as shown in below code.
public class PhoneCallReceiver extends BroadcastReceiver {
Context context = null;
private static final String TAG = "Phone call";
#Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(answer, null);
Log.d(TAG, "Answered incoming call from: " + number);
}
return;
}
}
This method works till Android 4.1 after which android has restricted user apps from broadcasting "Intent.ACTION_MEDIA_BUTTON".
So my conclusion is that currently there is no way how we can achieve this in Android 4.1 or later.
Has anybody else found any other solution or workaround to this problem?
This works from Android 2.2 to 4.0 and now after adding the try catch to the last line it works for 4.1.2 and 4.2 Frankly speaking dont know how it works but it works for me.
Log.d(tag, "InSecond Method Ans Call");
// 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));
sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0);
headSetUnPluggedintent.putExtra("name", "Headset");
try {
sendOrderedBroadcast(headSetUnPluggedintent, null);
} catch (Exception e) {
e.printStackTrace();
}
This is working for me in Android 4.1.2 as well as i have tested on 4.2
This still gives an exception which is handled.
Edit for End Call
Hope this helps all the people looking for total solution for answer and end call.
/**
* Reject button click listener will reject the incoming call.
*/
private class RejectCallOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
Log.d(tag, "OnRejectButton: " + "Reject OnClick");
ignoreCall();
exitCleanly();
}
}
/**
* ignore incoming calls
*/
private void ignoreCall() {
if (USE_ITELEPHONY)
ignoreCallAidl();
else
ignoreCallPackageRestart();
}
/**
* AIDL/ITelephony technique for ignoring calls
*/
private void ignoreCallAidl() {
try {
// telephonyService.silenceRinger();
telephonyService.endCall();
} catch (RemoteException e) {
e.printStackTrace();
Log.d(tag, "ignoreCall: " + "Error: " + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
Log.d(tag, "ignoreCall" + "Error: " + e.getMessage());
}
}
/**
* package restart technique for ignoring calls
*/
private void ignoreCallPackageRestart() {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
am.restartPackage("com.android.providers.telephony");
am.restartPackage("com.android.phone");
}
/**
* cleanup and exit routine
*/
private void exitCleanly() {
unHookReceiver();
this.finish();
}
My application has been using the following code to answer the phone for about 6 months:
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(i, null);
I have tested this on Android Versions from 2.2 to 4.2.2. I have not seen a SecurityException broadcasting "Intent.ACTION_MEDIA_BUTTON" in my testing n a 4.2.2 device, nor have I seen crash reports from the Play Store indicating such exceptions are occurring.
I will say that this does not always work. It does not work on HTC devices due to the fact that HTC devices have a HeadsetObeserver that listen for the actually plugging in of a wired headset. Without this event, which it is currently a SecurityException for a third party app to broadcast, the HeadsetHook KeyEvent goes ignored.
The previous answers are misleading. The following code block does nothing:
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 0);
headSetUnPluggedintent.putExtra("name", "Headset");
try {
sendOrderedBroadcast(headSetUnPluggedintent, null);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
except generate a SecurityException and catch it.
In the other answers where the code works, it is because of the KeyEvent.KEYCODE_HEADSETHOOK being broadcast.
Try this
Answer for end the call using pro grammatically. Its working fine for me.
try {
String serviceManagerName = "android.os.ServiceManager";
String serviceManagerNativeName = "android.os.ServiceManagerNative";
String telephonyName = "com.android.internal.telephony.ITelephony";
Class telephonyClass;
Class telephonyStubClass;
Class serviceManagerClass;
Class serviceManagerStubClass;
Class serviceManagerNativeClass;
Class serviceManagerNativeStubClass;
Method telephonyCall;
Method telephonyEndCall;
Method telephonyAnswerCall;
Method getDefault;
Method[] temps;
Constructor[] serviceManagerConstructor;
// Method getService;
Object telephonyObject;
Object serviceManagerObject;
telephonyClass = Class.forName(telephonyName);
telephonyStubClass = telephonyClass.getClasses()[0];
serviceManagerClass = Class.forName(serviceManagerName);
serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
Method getService = // getDefaults[29];
serviceManagerClass.getMethod("getService", String.class);
Method tempInterfaceMethod = serviceManagerNativeClass.getMethod(
"asInterface", IBinder.class);
Binder tmpBinder = new Binder();
tmpBinder.attachInterface(null, "fake");
serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
telephonyObject = serviceMethod.invoke(null, retbinder);
//telephonyCall = telephonyClass.getMethod("call", String.class);
telephonyEndCall = telephonyClass.getMethod("endCall");
//telephonyAnswerCall = telephonyClass.getMethod("answerRingingCall");
telephonyEndCall.invoke(telephonyObject);
} catch (Exception e) {
e.printStackTrace();
Log.error(DialerActivity.this,
"FATAL ERROR: could not connect to telephony subsystem");
Log.error(DialerActivity.this, "Exception object: " + e);
}
As a conclusion to this thread, here is the code that works for me for Android 4.2.2.
--> Call is answered by simulating push of headset hook and keeping the broadcast in try-catch as mentioned by #PravinDodia in abouve thread. (Observe that an exception is thrown and handled in catch and the call is answered anyway. So I guess we can just ignore this exception and continue living life as if nothing happened! )
--> Call is disconnected using ITelephony.
public class PhoneCallReceiver extends BroadcastReceiver {
Context context = null;
private static final String TAG = "Phone call";
#Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
return;
else {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
answerPhoneHeadsethook(context, intent);
return;
}
else if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
Log.d(TAG, "CALL ANSWERED NOW!!");
try {
synchronized(this) {
Log.d(TAG, "Waiting for 10 sec ");
this.wait(10000);
}
}
catch(Exception e) {
Log.d(TAG, "Exception while waiting !!");
e.printStackTrace();
}
disconnectPhoneItelephony(context);
return;
}
else {
Log.d(TAG, "ALL DONE ...... !!");
}
}
}
public void answerPhoneHeadsethook(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG, "Incoming call from: " + number);
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
try {
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
Log.d(TAG, "ACTION_MEDIA_BUTTON broadcasted...");
}
catch (Exception e) {
Log.d(TAG, "Catch block of ACTION_MEDIA_BUTTON broadcast !");
}
Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone
headSetUnPluggedintent.putExtra("name", "Headset");
// TODO: Should we require a permission?
try {
context.sendOrderedBroadcast(headSetUnPluggedintent, null);
Log.d(TAG, "ACTION_HEADSET_PLUG broadcasted ...");
}
catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Log.d(TAG, "Catch block of ACTION_HEADSET_PLUG broadcast");
Log.d(TAG, "Call Answered From Catch Block !!");
}
Log.d(TAG, "Answered incoming call from: " + number);
}
Log.d(TAG, "Call Answered using headsethook");
}
public static void disconnectPhoneItelephony(Context context) {
ITelephony telephonyService;
Log.v(TAG, "Now disconnecting using ITelephony....");
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Log.v(TAG, "Get getTeleService...");
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
//telephonyService.silenceRinger();
Log.v(TAG, "Disconnecting Call now...");
//telephonyService.answerRingingCall();
//telephonyService.endcall();
Log.v(TAG, "Call disconnected...");
telephonyService.endCall();
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG,
"FATAL ERROR: could not connect to telephony subsystem");
Log.e(TAG, "Exception object: " + e);
}
}
}
At least the disconnect functionality works and we know how it works. So those who want to develop a Call Barring application can go ahead.
For those like me who want to answer a call, I guess we can use this for now and only hope that it does not stop working in the next version.
Try this :
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");
Add permissions in AndroidManifest.xml file as
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>
ITelephony method does not work on 4.4 and I find the headset/media button method still allows a fairly long ring before hangup.
This chaps blog post shows a new method that I have tested as working on 4.4.2 Galaxy s4 and HTC one mini that hangs up much more quickly and you don't get a missed call entry either.
http://aprogrammersday.blogspot.co.uk/2014/05/disconnect-block-drop-calls-android-4.html
The technique uses a runtime exec as below, apparently you may need to use a different number for some devices.
public class HangupPhoneCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (TelephonyManager.EXTRA_STATE_RINGING.equals(intent.getStringExtra(TelephonyManager.EXTRA_STATE))) {
Executor eS = Executors.newSingleThreadExecutor();
eS.execute(new Runnable() {
#Override
public void run() {
Runtime runtime = Runtime.getRuntime();
try {
Log.d(TAG, "service call phone 5 \n");
runtime.exec("service call phone 5 \n");
} catch (Exception exc) {
Log.e(TAG, exc.getMessage());
}
}
});
return;
}
}
}
Call disconnecting using IT Telephony doesn't work on some devices like Samsung S Duos. But you can still make ringer silent :)
To end call in older version then 9.0
use this:
TelephonyManager tm = (TelephonyManager)context.getSystemService(TELEPHONY_SERVICE);
Method m1 = null;
try {
m1 = tm.getClass().getDeclaredMethod("getITelephony");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
m1.setAccessible(true);
Object iTelephony = null;
try {
iTelephony = m1.invoke(tm);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Method m3 = null;
try {
m3 = iTelephony.getClass().getDeclaredMethod("endCall");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
m3.invoke(iTelephony);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
& for pie
TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if (telecomManager != null) {
return telecomManager.endCall();
}
Make sure your compile SDK version is 28
private void PhoneControl(int nControl) {
if(nControl == PHONE_END_CALL) { // End call, all Android version
try {
TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if (telecomManager != null) {
telecomManager.endCall();
}
}catch (Exception e) {}
try {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(tm == null)
return;
tm.getClass().getMethod("endCall").invoke(tm);
bIsEnding = true;
} catch (Exception e) {
/* Do Nothing */ }
}
if(nControl == PHONE_ACCEPT_CALL) { // Accept phone call
// if(!bCallAccepted) { // Call déjà accepté => pas d'action (évite double action)
bCallAccepted = true;
if(Build.VERSION.SDK_INT >= 26) { // Pris en charge Android >= 8.0
if(context.checkSelfPermission("android.permission.ANSWER_PHONE_CALLS") == PackageManager.PERMISSION_GRANTED) {
TelecomManager tm = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
if(tm != null)
tm.acceptRingingCall();
}
}
if(Build.VERSION.SDK_INT >= 23 && Build.VERSION.SDK_INT < 26) { // Hangup in Android 6.x and 7.x
MediaSessionManager mediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
if(mediaSessionManager != null) {
try {
List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions
(new ComponentName(context, NotificationReceiverService.class));
for (MediaController m : mediaControllerList) {
if ("com.android.server.telecom".equals(m.getPackageName())) {
m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
break;
}
}
} catch (Exception e) {
// Toast.makeText(instance, "sdfsdf123123"+e, Toast.LENGTH_LONG).show();
Intent answerCalintent = new Intent(context, AcceptCallActivity.class);
answerCalintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(answerCalintent);
/* Do Nothing */ }
}
// new Thread(new Runnable() {
//
// #Override
// public void run() {
// try {
// Runtime.getRuntime().exec("input keyevent " +
// Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
// } catch (IOException e) {
// // Runtime.exec(String) had an I/O problem, try to fall back
// String enforcedPerm = "android.permission.CALL_PRIVILEGED";
// Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
// Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
// KeyEvent.KEYCODE_HEADSETHOOK));
// Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
// Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
// KeyEvent.KEYCODE_HEADSETHOOK));
//
// context.sendOrderedBroadcast(btnDown, enforcedPerm);
// context.sendOrderedBroadcast(btnUp, enforcedPerm);
// }
// }
//
// }).start();
}
if(Build.VERSION.SDK_INT < 23) { // Prend en charge jusqu'à Android 5.1
try {
if(Build.MANUFACTURER.equalsIgnoreCase("HTC")) { // Uniquement pour HTC
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if(audioManager!=null && !audioManager.isWiredHeadsetOn()) {
Intent i = new Intent(Intent.ACTION_HEADSET_PLUG);
i.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
i.putExtra("state", 0);
i.putExtra("name", "Orasi");
try {
context.sendOrderedBroadcast(i, null);
} catch (Exception e) { /* Do Nothing */ }
}
}
Runtime.getRuntime().exec("input keyevent " +
Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
} catch (Exception e) {
// Runtime.exec(String) had an I/O problem, try to fall back
String enforcedPerm = "android.permission.CALL_PRIVILEGED";
Intent btnDown = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK));
Intent btnUp = new Intent(Intent.ACTION_MEDIA_BUTTON).putExtra(
Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
context.sendOrderedBroadcast(btnDown, enforcedPerm);
context.sendOrderedBroadcast(btnUp, enforcedPerm);
}
}
// }
}
}
<!--Incoming call state Listen-->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- Store Data in Device/Shared pref-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--Get Incoming Number-->
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<!--Answer call-->
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission
android:name="android.permission.MODIFY_PHONE_STATE"
tools:ignore="ProtectedPermissions" />
<!--End Calll-->
<uses-permission android:name="android.permission.CALL_PHONE" />
public class NotificationReceiverService extends NotificationListenerService {
public NotificationReceiverService() {
}
}
<service
android:name=".notifications.NotificationReceiverService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
if(Build.VERSION.SDK_INT >= 26) { // Permission necessaire
if(checkSelfPermission("android.permission.ANSWER_PHONE_CALLS") != PackageManager.PERMISSION_GRANTED) {
String szPermissions[] = {"android.permission.ANSWER_PHONE_CALLS"};
requestPermissions(szPermissions, 0);
}
}
if(Build.VERSION.SDK_INT < 26 && Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1 )
{ // Permission pour Android 6.x et 7.x
ContentResolver contentResolver = getContentResolver();
String enabledNotificationListeners = Settings.Secure.getString(contentResolver, "enabled_notification_listeners");
String packageName = getPackageName();
if (enabledNotificationListeners == null || !enabledNotificationListeners.contains(packageName)) {
Intent intent2 = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
intent2 = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
}
startActivity(intent2);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
Intent intent=new Intent(MainActivity.this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}, 1000);
}
}
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);
}
}
}