Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know whether I am in a call on Android?

I want to know whether I am in a call.

If I am in a call then start the service (service part is clear). How do I do this?

While attending the call I need to call the service... I am unaware of how to do this? Any help?

like image 824
Rockin Avatar asked May 10 '11 10:05

Rockin


People also ask

How do you know if someone is on the phone when you call?

If you see a red phone icon on their phone number, then this person is most likely available and using their phone. However, if there is a red bell displayed instead, their phone might be on Silent or Do Not Disturb and they might be away from their device.

How do I know my incoming calls?

Open your device's Phone app . Tap Recents . You'll see one or more of these icons next to each call in your list: Missed calls (incoming) (red)


1 Answers

You need broadcast receiver ...

In manifest declare broadcast receiver ...

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

Also declare uses-permission ...

<uses-permission android:name="android.permission.READ_PHONE_STATE" />   

The broadcast receiver class ...

package x.y;  import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager;  public class PhoneStateBroadcastReceiver extends BroadcastReceiver{      @Override     public void onReceive(Context context, Intent intent) {          TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);         telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);      }  } 

And one class to customize phone state listener...

package x.y; import android.content.Context; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager;  public class CustomPhoneStateListener extends PhoneStateListener {      //private static final String TAG = "PhoneStateChanged";     Context context; //Context to make Toast if required      public CustomPhoneStateListener(Context context) {         super();         this.context = context;     }      @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:             //when Ringing             Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();             break;         default:             break;         }     } } 
like image 110
Vaibhav Jani Avatar answered Sep 24 '22 05:09

Vaibhav Jani