Created
August 17, 2015 06:11
-
-
Save Videmor/19091cbfe9ec7380f6d7 to your computer and use it in GitHub Desktop.
android PhoneStateListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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