Disconnect Incoming call programmatically - android

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" />

Related

Ending a phone call in Android API level 30

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();
}
}
}
}

blocking specific numbers (call and sms) in android

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?

Keep the current Activity in front while call come

Backgroud
I am developing an app which makes calls like lollpop. When a call comes it should show a popup on the screen and the default calling screen should not be visible.
What I have done
I have build the broadcast receiver and currently I am able to show a popup every time a call come.
by using this code
public class PhoneCallReceive extends BroadcastReceiver {
Class<?> c;
public static ITelephony telephonyService = null;
TelephonyManager telephony;
Method m;
public static int flag = 0;
public static AudioManager audioManager;
#Override
public void onReceive(Context conk, Intent inter) {
final Context context = conk;
final Intent intent = inter;
Thread pageTimer = new Thread() {
public void run() {
try {
sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
try {
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
c = Class.forName(telephony.getClass().getName());
m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
} catch (Exception e) {
e.printStackTrace();
}
if (telephony.getCallState() == 1)
flag = 1;
Log.d("Rec", "reciever");
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
callingscreen("1", context, intent);
}
}
};
pageTimer.start();
}
public void callingscreen(final String type, final Context context,
final Intent intent) {
Intent intentPhoneCall = new Intent(context.getApplicationContext(),
CallerScreen.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentPhoneCall.putExtra(
"number",
intent.getExtras().getString(
TelephonyManager.EXTRA_INCOMING_NUMBER));
intentPhoneCall.putExtra("type", type);
context.startActivity(intentPhoneCall);
}
} // class
Problem
But the Problem is that this popup is showing over the default calling screen. What I want to show the popup on the user current screen on which he was working.
Question
So the simple question is that how can I get the user current screen with its all functionality below the popup?
Please help:)
This solution works on every version of android, including lollipop
Use this broadcast reciever
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING, call intent here
Intent i = null;
i = new Intent(context, My_activity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
break;
default:
break;
}
}
}
and register it like this in onCreate
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
And yes, please dont miss
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Cheers! Your problems is solved ;)

Is it possible to generate alert or any UI before incoming and outgoing calls and texts in android

In my application I need to generate an alert or something of that kind which asks the user for confirmation before outgoing and incoming calls and texts. I'm done with NEW_OUTGOING_CALL and I can just Toast a msg.
You can use ITelephone.aidl and place events to listen the phone state. I am helping you for the Telephone calls handling (Including a short snippet about how to disconnect it). SMS also happens in similar way. It would be best if you can research and figure out about it
import com.android.internal.telephony.ITelephony;
import android.os.IBinder;
import android.widget.Toast;
import android.telephony.TelephonyManager;
import android.telephony.PhoneStateListener;
public class CallMonitor
{
protected StateListener phoneStateListener;
//stops the service to monitor any call.
public void stopMonitor()
{
try
{
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
phoneStateListener=null;
Toast.makeText(this, "Call Monitoring Stopped", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
}
}
public void startMonitor()
{
try
{
phoneStateListener = new StateListener();
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
Toast.makeText(this, "Call Monitoring Started", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
#Override
public IBinder onBind(Intent arg0)
{
return null;
}
class StateListener extends PhoneStateListener
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
Context context = getApplicationContext();
try
{
TelephonyManager manager = (TelephonyManager)context.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)
{
Toast.makeText(context, "Error ending the call" + e.getMessage(), Toast.LENGTH_LONG).show();
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
}
Permissions are
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
I have not worked in this area before but I think, You can use PhoneStateListener interface.. which will invoke functions as the phone state changes....The Telephony Manager might be useful too.
Just a try from my side hope that helped,

How to disconnect incoming call in android by programatically

I want to disconnect incoming call for a particular time.
How can i do this is it impossible ?
I searched and i find it is impossible. please help me.
public class Telephony3 extends Service
{
Context context;
public IBinder onBind(Intent intent)
{
return null;
}
public void onCreate()
{
super.onCreate();
try
{
StateListener phoneStateListener = new StateListener();
TelephonyManager telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telephonymanager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}catch(Exception e)
{
e.printStackTrace();
}
}
class StateListener extends PhoneStateListener{
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
//Disconnect the call here...
try{
TelephonyManager manager = (TelephonyManager)context.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){
Log.d("",e.getMessage());
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
}
}
};
public void onDestroy()
{
}
}
///////////////////////////////////////////
make another package com.android.internal.telephony and make one interface ITelephony
public interface ITelephony
{
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
and when i call this emulator from another the calling is happening. any thing wrong here please suggest me.
thanks in advance
You can generate your own activity that handles action "ACTION_ANSWER".
Register your application component as a intent handler.
Here is a blog post which suggest a possible workaround.
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
getContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
from
http://androidbridge.blogspot.com/2011/05/how-to-answer-incoming-call-in-android.html

Categories

Resources