blocking Incoming call - Android - android

i have incoming call broadcast receiver and phone state listener class , how to block or reject incoming call and the ringing notification
BroadCastReceiver
public class PhoneCallReceiver extends BroadcastReceiver{
private static final String TAG = "Phone call";
#Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Receving....");
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
SamplePhoneStateListener customPhoneListener = new SamplePhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
Phone Listener
public class SamplePhoneStateListener extends PhoneStateListener {
private static final String TAG = "Phone Listner";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.v(TAG, "Current State is - "+state);
Log.v(TAG, incomingNumber);
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "Phone Ringigg");
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}

make sure you have the permission android.permission.READ_PHONE_STATE set up.

To block a call u have to use java reflections and access hidden file of android.telephony package. this file (ITelephony.java) has a function called endcall() which uses permissions. this is not advised by google team as recieving and ending calls in a programmatic way is invasion of user privacy.

Related

Ending a phone call in Android

After a phone call ends, I must open another Activity.
However, it doesn't catch the "call end signal".
I've used mTelManager in BroadcastReceiver and Service, but it doesn't work.
When Activity is opened by a Service or BroadcastReceiver, it shows the MainActivity and not the targeted one instead.
Source code:
public class MainService extends Service{
TelephonyManager mTelManager;
PhoneStateRead pListener;
String TAG = "Call State catch";
boolean callOutState = false; //calling state
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
Log.i(TAG, "ServiceReceiver->onReceive();");
pListener = new PhoneStateRead();
mTelManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelManager.listen(pListener, PhoneStateListener.LISTEN_CALL_STATE);
if(callOutState){ //if call end
Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showRecord);//open Call_Out_Record_Activity
}
}
public class PhoneStateRead extends PhoneStateListener {
private final String TAG = "Phone State Read";
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_IDLE "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_OFFHOOK "+incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> CALL_STATE_RINGING "+incomingNumber);
break;
default:
Log.i(TAG,"MyPhoneStateListener->onCallStateChanged() -> default -> "+Integer.toString(state));
break;
}
CatchCallState(state);
}
}
private void CatchCallState(int state){
if(state == TelephonyManager.CALL_STATE_OFFHOOK || state == TelephonyManager.CALL_STATE_RINGING)
callOutState = false;
else if(state == TelephonyManager.CALL_STATE_IDLE)
callOutState = true;
}
}
So it looks like you have a couple problems here. First, you are trying to start your activity when you start your Service - you should do this code:
if(callOutState){ //if call end
Intent showRecord = new Intent(getBaseContext(), Call_Out_Record_Activity.class);
showRecord.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(showRecord);//open Call_Out_Record_Activity
}
in the CallStateListener. Or just put it in your CatchCallState method.
Next, notice that there is not a "call end signal" - the phone will go from "OFF_HOOK" to "IDLE" so you need flags to track that. Also, notice that "RINGING" happens on incoming calls, but not outgoing calls. So you may want to display different messages depending on incoming or outgoing calls.

setRingerMode to normall during incoming call doesn't work on Samsung devices

I've read here. Didn't find the answer.
public class CallReceiver extends BroadcastReceiver {
private TelephonyManager mTelephony;
private IncomingPhoneStateListener mIncomingPhoneListener;
private String number;
private Context mContext;
#Override
public void onReceive(Context context, Intent intent) {
try {
mContext = context;
number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
mIncomingPhoneListener = new IncomingPhoneStateListener();
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("CallReceiver", "onReceive error:" + e.getMessage());
}
}
public class IncomingPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// ******* This code should unmute and change volume to MAX
am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), AudioManager.FLAG_PLAY_SOUND);
// ******* end
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
mTelephony.listen(mIncomingPhoneListener, PhoneStateListener.LISTEN_NONE); // unregister
}
}
}
When the device is on silent mode or vibration mode, above code unmute and change the volume to the max.
This code works on Nexus devices (eg. Galaxy Nexus)
However, it doesn't work on Samsung devices (S3, S4, Note 2) [Android version 4.3].
Any suggestions?
Since I couldn't find the answer, I came up with the work-around by actually playing the ringer sound in my service.

I am trying to delete a particular incoming number after hang up but these code not working properly

I am trying to delete a particular incoming number from call log when phone hang-up. I tried as i can do but that code not working the number still remain in call log.
it is working properly when broadcast invoke but this is not deleting number of incoming call.
Thanks in Advance.
public class ListenPhoneState extends BroadcastReceiver {
private boolean isPressOffHock = false;
static private String incomingNumberToDelete;
#Override
public void onReceive(Context context, Intent intent) {
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
if(incomingNumberToDelete != null)
{
String queryString="NUMBER="+incomingNumberToDelete;
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI,queryString,null);
Log.d("Delete", "Delete Number " + incomingNumberToDelete );
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
if(state == TelephonyManager.CALL_STATE_RINGING)
{
incomingNumberToDelete = incomingNumber;
Log.d("Check", "income " + incomingNumberToDelete );
}
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
//Log.d("IDLE", state + "incoming" + incomingNumber);
isPressOffHock = true;
break;
case TelephonyManager.CALL_STATE_RINGING:
//Log.d("RINGING", state + "incoming" + incomingNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
// Log.d("CALL_STATE_OFFHOOK",msg );
break;
}
}
}
}
Usually there is a delay between the broadcast of a phone state and the system data that is written or system activities (such as call logs or phone ringing). Right now, your receiver is probably trying to delete the call log before it is present.
You could delay the delete or take a look at this post for registering a ContentObserver:
Delete call from call log after call end

Trouble with reading phone state

I want to perform some operation (Pause game) in my application when a call came. But reading the phone state is not working. I have given permission(READ_PHONE_STATE) in the manifest. Nothing is happen when a call came.
Thanks.
TelephonyManager telephonyManager;
PhoneStateListener listener;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
*
*
*
listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(SudokuGameActivity.this, "IDLE", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(SudokuGameActivity.this, "OFF Hook", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(SudokuGameActivity.this, "Ringing", Toast.LENGTH_SHORT).show();
mpauseButton.performClick();
break;
}
}
};
Have you written the following line :
telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
when your listener has be created, you need invoke `public void listen (PhoneStateListener listener, int events)' to listen.
also, you can try this:
create a broadcatst receiver handle the action android.intent.action.PHONE_STATE,
code example:
public class PhoneStateReceiver extends BroadcastReceiver {
private TelephonyManager manager;
#Override
public void onReceive(Context context, Intent intent) {
if (manager == null) {
manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
String action = intent.getAction();
System.out.println(action);
System.out.println("current phone state:" + manager.getCallState());
}
}

Broadcast receiver and phonestatelistener

I am trying to use BroadcastReceiver to receive incoming calls. I want to register call when the call is ended. Below code gives each state (idle,ringing n offhook) more than once with the same number (its not an issue of past call). Is it because phonestatelistner is called within the service.
public class IncomingCallReceiver extends BroadcastReceiver {
int stateString =0;
int prevStat = 0;
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String p = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
prevStat = stateString;
stateString = 1;
Log.i("ON CALL STATE CHANGED","IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("ON CALL STATE CHANGED","OFFFHOOK");
prevStat = stateString;
stateString = 2;
break;
case TelephonyManager.CALL_STATE_RINGING:
prevStat = stateString;
stateString = 3;
Log.i("ON CALL STATE CHANGED","RINGING");
break;
}
if(prevStat == 2 && stateString == 1)
{
Log.i("REGISTER A CALLL","==" +p);
}
}
};
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
I also tried the same thing with phonestatelistener in a separate class, but it was not being called, i.e my code didn't reach there. In my manifest file I have registered receiver, but there is no declaration of phonestatelistener. Is that to be registered?
I am sorry if the solution is quite obvious but am new to android things.
Any help is appreciated :)
Yes, choose listener or received , not both of them

Categories

Resources