Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to identify incoming call and outgoing call in android

how to get the events of incoming call and outgoing call in android separately. Actually i was trying to develop an application that open on incoming call if number is exist in database and it work OK. but if i call from the device(outgoing call) and if number is exist in database still it open my application. i want to restrict to open my application on outgoing call.

my manifest contain i receive incoming call like this:

<receiver android:name=".IncomingCallReceiver" >
    <intent-filter >             
        <action android:name="android.intent.action.PHONE_STATE" />             
    </intent-filter>
</receiver>

IncomingCallReceiver:

MyPhoneStateListener phoneListener=new MyPhoneStateListener(context);
TelephonyManager telephony = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);

MyPhoneStateListener:

public void onCallStateChanged(int state,String incomingNumber){
      switch(state) {
          case TelephonyManager.CALL_STATE_IDLE:
              Log.d("DEBUG", "IDLE");
          break;
          case TelephonyManager.CALL_STATE_OFFHOOK:
              Log.d("DEBUG", "OFFHOOK");                        
              Intent i = new Intent(context, MyMainActivity.class);
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              context.startActivity(i);
          break;
          case TelephonyManager.CALL_STATE_RINGING:
              Log.d("DEBUG", "RINGING");
          break;
    }
}

can anyone help me to differentiate outgoing call from incoming call so i'll handle this events.

Thanks in advance.

like image 531
Hiren Dabhi Avatar asked Sep 20 '11 12:09

Hiren Dabhi


People also ask

How do I find incoming and outgoing calls on android?

Detecting incoming and outgoing calls in Android can be realized in several ways. One of the possibilities is to use a custom PhoneStateListener which can be attached to the TelephonyManager in your onReceive() function of the custom BroadcastReceiver.

How do you identify a caller?

If you received a call and need to know the caller's identity, the classic way to find out is by using call return and reconnecting with the caller. To use this service, supported by many landlines, VoIP services, and cell phones, you just need to dial * followed by 69.

How do I check outgoing calls on my android phone?

Go to the main menu on your phone. Find Call History and go to Call Time, where you can check the most recent calling time, and the calling time for calls received and made respectively, along with the total calling time.


2 Answers

i want to restrict to open my application on outgoing call.

on case TelephonyManager.CALL_STATE_OFFHOOK: check if the previous state was CALL_STATE_RINGING or CALL_STATE_IDLE (for example by settings a different flag in both cases).
In the latter case proceed with opening your application, else do nothing

like image 85
DonGru Avatar answered Oct 05 '22 12:10

DonGru


add <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> in your receiver and process it in your onReceive() method.

<receiver android:name=".IncomingCallReceiver" >
    <intent-filter >             
       <action android:name="android.intent.action.PHONE_STATE" />
       <action android:name="android.intent.action.NEW_OUTGOING_CALL" />             
    </intent-filter>
</receiver>
like image 21
Vineet Shukla Avatar answered Oct 05 '22 12:10

Vineet Shukla