Skip to content

Instantly share code, notes, and snippets.

@Videmor
Created August 17, 2015 06:11
Show Gist options
  • Select an option

  • Save Videmor/19091cbfe9ec7380f6d7 to your computer and use it in GitHub Desktop.

Select an option

Save Videmor/19091cbfe9ec7380f6d7 to your computer and use it in GitHub Desktop.
android PhoneStateListener
TextView text;
public String num = "num";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
text = (TextView) findViewById(R.id.text);
text.setText(num);
// try{
// text.setText(TelephonyMgr.getLine1Number());
//
// }
// catch(NullPointerException ex){
// text.setText(TelephonyMgr.getSubscriberId());
//
// }
}
class TeleListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// CALL_STATE_IDLE;
Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE",
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// CALL_STATE_OFFHOOK;
Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK",
Toast.LENGTH_LONG).show();
text.setText(incomingNumber);
num = incomingNumber;
break;
case TelephonyManager.CALL_STATE_RINGING:
// CALL_STATE_RINGING
Toast.makeText(getApplicationContext(), incomingNumber,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING",
Toast.LENGTH_LONG).show();
text.setText(incomingNumber);
num = incomingNumber;
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment