Android provide us the facility to know the call state. Android gives this feature by providing Telephonymanager class. We need to implement PhoneStateListener interface that has one method onCallStateChanged().
Below is the example.
public class PhoneStateActivity extends Activity {
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_state);
TelephonyManager myTM =
(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
PhoneStateListener phoneStateListener= new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber)
{
if(state==TelephonyManager.CALL_STATE_RINGING){
Toast.makeText(PhoneStateActivity.this,Hey, receive your call. Phone is ringing.",
Toast.LENGTH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
Toast.makeText(PhoneStateActivity.this,"You are in a call. ",
Toast.LENGH_LONG).show();
}
if(state==TelephonyManager.CALL_STATE_IDLE){
Toast.makeText(PhoneStateActivity.thisYou are in idle state ",
Toast.LENGTH_LONG).show();
}
}
};
myTM.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
}
}
0 Comment(s)