I want to make a call with my program and after that recognize the call status . I want to detect the end of outgoing ringtone.
How to detect the end of outgoing ringtone ?
I use of Flowing code for make call .
EditText ed = (EditText) findViewById(R.id.txtcall);
Uri uri = Uri.fromParts("tel", ed.getText().toString(), null);
callIntent = new Intent(Intent.ACTION_CALL,uri);
startActivity(callIntent);
Following code snippet can help you in finding the phone call current statue whether its picked, ringing or idle. You can easily use these states and implement your functionality accordingly.
private class CustomPhoneStateListener extends PhoneStateListener
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
//TelephonyManager.CALL_STATE_IDLE
//TelephonyManager.CALL_STATE_OFFHOOK
//TelephonyManager.CALL_STATE_RINGING
}
}
Check by This:- you will be able to call user
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+9189745847"));
startActivity(callIntent);
and also use in androidmanifest.xml file:
<uses-permission android:name="android.permission.CALL_PHONE" />
Have a look at intent NEW_OUTGOING_CALL
<receiver android:name=".receiver.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
Then you can check the state of call with a PhoneStateListener :
public class OutgoingCallReceiver extends BroadcastReceiver {
protected static final String CLASS_TAG = "OutgoingCallReceiver : ";
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle) return;
String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(LOG_TAG, CLASS_TAG + "phone number " + phonenumber);
final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.d(LOG_TAG, CLASS_TAG + "RINGING");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(LOG_TAG, CLASS_TAG + "OFFHOOK");
Process.onCallStateIsOFFHOOK();
break;
case TelephonyManager.CALL_STATE_IDLE:
Process.onCallStateIsIDLE();
Log.d(LOG_TAG, CLASS_TAG + "IDLE");
break;
default:
Log.d(LOG_TAG, CLASS_TAG + "Default: " + state);
break;
}
}
}, PhoneStateListener.LISTEN_CALL_STATE);
}
}
In the Process class you can detect the ringtone and create more state like CALLBEGINS, CALLANSWERED by recording the VOICE_CALL.
Example of onCallStateIsIDLE() :
public void onCallStateIsIDLE() {
setSpeakingOn(false);
if (stateCall == STATE_CALLANSWERED ||
stateCall == STATE_CALLBEGINS ||
stateCall == STATE_DIALISOFFHOOK ||
stateCall == STATE_DIALENDREQUESTED) {
Log.i(LOG_TAG, CLASS_TAG + " - forcing state : STATE_DIALREADY : old state = " + stateCall);
stateCall = STATE_DIALREADY;
}
}
Related
I Want to do call forwarding from my application the scenario is when any GSM call came then from my application i want to forward that number to other desitination number.
Here i have done some code for Call forwarding but its not working. I have created Receiver and also declared in Manifest and has given Call permission.
But anyhow it is not working.
Manifests Code:-
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Receiver Code :-
public class PhoneStateReceiver extends BroadcastReceiver {
Context ctx;
#Override
public void onReceive(Context context, Intent intent) {
this.ctx=context;
try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("Incoming Number" + incomingNumber);
System.out.println("INcoming State" + state);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
Toast.makeText(context,"Incoming Call State",Toast.LENGTH_SHORT).show();
Toast.makeText(context,"Ringing State Number is -"+incomingNumber,Toast.LENGTH_SHORT).show();
String fwdMobNumVar = ("**21*" + "1234567890" + "#");
callforward(fwdMobNumVar);
}
if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))){
Toast.makeText(context,"Call Received State",Toast.LENGTH_SHORT).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
Toast.makeText(context,"Call Idle State",Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
e.printStackTrace();
}
}
private void callforward(String callForwardString) {
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager)
ctx.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Intent intentCallForward = new Intent(Intent.ACTION_CALL);
Uri mmiCode = Uri.fromParts("tel", callForwardString, ("#"));
intentCallForward.setData(mmiCode);
ctx.startActivity(intentCallForward);
}
public class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
if (TelephonyManager.CALL_STATE_RINGING == state)
{
// phone ringing
Toast.makeText(ctx,"Callforward ringing",Toast.LENGTH_SHORT).show();
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state)
{
// active
Toast.makeText(ctx,"Callstate hook",Toast.LENGTH_SHORT).show();
isPhoneCalling = true;
}
if (TelephonyManager.CALL_STATE_IDLE == state)
{
// run when class initial and phone call ended, need detect flag
// from CALL_STATE_OFFHOOK
if (isPhoneCalling)
{
// restart app
Toast.makeText(ctx,"Call phone forwading ",Toast.LENGTH_SHORT).show();
Intent i = ctx.getPackageManager()
.getLaunchIntentForPackage(ctx.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
ctx.startActivity(i);
isPhoneCalling = false;
}
}
}
}
}
Is there any state which can tell us that the line is connected and other person's phone is ringing. Like I want to know the state where proper connection is made and other person knows you are calling and can answer it?
I have tried this but it never goes in phone ringing state:
public void onReceive(final Context context, Intent intent) {
TelephonyManagerTm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Tm.listen(new PhoneStateListener(){
public void onCallStateChanged(int state,String number) {
super.onDataConnectionStateChanged(state);
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "Phone Ringing", 1).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "call rejected", 1).show();
break;
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
Even though there are only three states available in Android Telephony Manager, it is very easy to identify the waiting state of a new incoming call. And this algorithm will provide state for all situations in a phone call.
So here we are receiving the 3 events from android such as RINGING, OFFHOOK and IDLE. And in order to get the waiting state of a new incoming call, we need to define our own states like RINGING, OFFHOOK, IDLE, FIRST_CALL_RINGING, SECOND_CALL_RINGING.
Please think in a way that we are receiving events from android and we will define our on call states.
Here is the method to get call states at onReceive() of broadcast-receiver without registering PhoneStateListener and escaping from other complications. See the code.
public class CallListening extends BroadcastReceiver {
static CustomPhoneStateListener customPhoneListener;
private static final String TAG ="broadcast_intent";
public static String incoming_number;
private String current_state,previus_state,event;
public static Boolean dialog= false;
private Context context;
private SharedPreferences sp,sp1;
private SharedPreferences.Editor spEditor,spEditor1;
public void onReceive(Context context, Intent intent) {
//Log.d("intent_log", "Intent" + intent);
dialog=true;
this.context = context;
event = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
incoming_number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.d(TAG, "The received event : "+event+", incoming_number : " + incoming_number);
previus_state = getCallState(context);
current_state = "IDLE";
if(incoming_number!=null){
updateIncomingNumber(incoming_number,context);
}else {
incoming_number=getIncomingNumber(context);
}
switch (event) {
case "RINGING":
Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
current_state ="FIRST_CALL_RINGING";
}
if((previus_state.equals("OFFHOOK"))||(previus_state.equals("SECOND_CALL_RINGING"))){
current_state = "SECOND_CALL_RINGING";
}
break;
case "OFFHOOK":
Log.d(TAG, "State : offhook, incoming_number : " + incoming_number);
if((previus_state.equals("IDLE")) ||(previus_state.equals("FIRST_CALL_RINGING")) || previus_state.equals("OFFHOOK")){
current_state = "OFFHOOK";
}
if(previus_state.equals("SECOND_CALL_RINGING")){
current_state ="OFFHOOK";
startDialog(context);
}
break;
case "IDLE":
Log.d(TAG, "State : idle and incoming_number : " + incoming_number);
if((previus_state.equals("OFFHOOK")) || (previus_state.equals("SECOND_CALL_RINGING")) || (previus_state.equals("IDLE"))){
current_state="IDLE";
}
if(previus_state.equals("FIRST_CALL_RINGING")){
current_state = "IDLE";
startDialog(context);
}
updateIncomingNumber("no_number",context);
Log.d(TAG,"stored incoming number flushed");
break;
}
if(!current_state.equals(previus_state)){
Log.d(TAG, "Updating state from "+previus_state +" to "+current_state);
updateCallState(current_state,context);
}
}
public void startDialog(Context context) {
Log.d(TAG,"Starting Dialog box");
Intent intent1 = new Intent(context, NotifyHangup.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
public void updateCallState(String state,Context context){
sp = PreferenceManager.getDefaultSharedPreferences(context);
spEditor = sp.edit();
spEditor.putString("call_state", state);
spEditor.commit();
Log.d(TAG, "state updated");
}
public void updateIncomingNumber(String inc_num,Context context){
sp = PreferenceManager.getDefaultSharedPreferences(context);
spEditor = sp.edit();
spEditor.putString("inc_num", inc_num);
spEditor.commit();
Log.d(TAG, "incoming number updated");
}
public String getCallState(Context context){
sp1 = PreferenceManager.getDefaultSharedPreferences(context);
String st =sp1.getString("call_state", "IDLE");
Log.d(TAG,"get previous state as :"+st);
return st;
}
public String getIncomingNumber(Context context){
sp1 = PreferenceManager.getDefaultSharedPreferences(context);
String st =sp1.getString("inc_num", "no_num");
Log.d(TAG,"get incoming number as :"+st);
return st;
}
}
There are 3 states: IDLE, RINGING, OFFHOOK.
For incoming calls the normal flow is:
IDLE (start state),
RINGING (the phone is ringing),
OFFHOOK (the user picks up)
For outgoing calls the flow is:
IDLE (start state),
OFFHOOK (called only after the other person picks up)
There's no RINGING state for outgoing calls before they're picked up.
Yes we can get the state PhoneStateListener.
Whenever you extend a class from PhoneStateListener, you will get onCallStateChanged(), like below:
public class CustomPhoneStateListener extends PhoneStateListener {
ActivityManager activityManager;
Intent i1;
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
i1 = new Intent(context, TelephoneyWithoutToastActivity.class);
i1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//when Off hook i.e in call
//Make intent and start your service here
Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
ActivityManager localActivityManager = (ActivityManager) this.context.getSystemService("activity");
for (String str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString();; str = ((ActivityManager.RunningTaskInfo) localActivityManager.getRunningTasks(1).get(0)).topActivity.flattenToString()) {
if ((!str.contains("com.android.phone.InCallScreen")))
continue;
Log.d("IncomingCallPlus", "*****************************************************");
context.startActivity(i1);
return;
}
default:
break;
}
}
}
For reference check this.
In the event of outgoing calls, are being throws automatically the state of OFF HOOK
Hi guys, firstly my apologies for my english.
Well, in my application I'm monitoring every call, before and after. But just after of the call start, so I don't matter with "ringing" state, however my application is triggering a fake "off hook" state. When I make a call (outgoing call), my app is setting the state off hook immediately following the ringing. With this, I'm monitoring a call that I don't should, because that call can do not be answered.
Did someone already had this problem?
AndroidManifest.xml
<receiver
android:name=".CallStateBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name=".OutgoingCallBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
CallStateBroadcastReceiver:
CallStatePhoneStateListener phoneListener = new CallStatePhoneStateListener(context, intent);
TelephonyManager telephony = (TelephonyManager) context.getSystemService(
Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
CallStatePhoneStateListener:
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// do something
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// do something
break;
case TelephonyManager.CALL_STATE_RINGING:
// do something
break;
}
OutgoingCallBroadcastReceiver:
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle == null)
return;
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
// just save the number
}
Take a Static String eg:- prev_state and store the state of phone in that String at each case
example if u want to know how the outgoing call is disconnected then should do something like this,
if(state==TelephonyManager.CALL_STATE_IDLE)
{ Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_nr);
{
if((prev_state==TelephonyManager.CALL_STATE_OFFHOOK))
{
prev_state=state;
// do something when the Call is ended
}
}
}
hope it helps :)
i hope this is work for u.
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Log.d("main", "incoming call receiver.");
PowerManager pm = (PowerManager) c
.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.v("idle state", "CALL_STATE_IDLE");
// CALL_STATE_IDLE;
if (ring == true && callReceived == false && CheckMissCall.isReject==true) {
Log.v("missed call", "Missed call from : " + incomingNumber);
if(CheckMissCall.isShown)
{
c.stopService(new Intent(c, Unlock_hud.class));
}
flag = true;
if (prefs.getBoolean("main_state", true) )
{
Intent inter = new Intent(c, MissCall.class);
inter.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
inter.putExtra("CellNumber", incomingNumber);
c.startActivity(inter);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
callReceived = true;
Log.v("call received", "CALL_STATE_OFFHOOK " + incomingNumber);
break;
case TelephonyManager.CALL_STATE_RINGING:
ring = true;
// CALL_STATE_RINGING
Log.v("call ringing", "CALL_STATE_RINGING " + incomingNumber);
Log.d("flags", "flags: " + flag);
if (flag) {
//cut = true;
//flag = false;
CheckMissCall call = new CheckMissCall(c);
call.setName(incomingNumber);
call.setNumber4Sms(incomingNumber);
call.setContactPhoto();
Log.d("main", "state ringing");
prefs = PreferenceManager.getDefaultSharedPreferences(c);
if (!prefs.getBoolean("main_state", false)) {
return;
}
if (CheckMissCall.isRunning) {
return;
}
else {
Log.d("main", "EEEEEEEEEEEEEEEE: Unlock hud");
Intent in = new Intent(c, Unlock_hud.class);
in.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
c.startService(in);
// c.stopService(new Intent(c, Unlock_hud.class));
}
}
break;
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());
}
}
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