I want to show a notification when a call is active.
I have done the first easy part. Notification starts when my call intent starts. Now to the tricky part, how can I tell my Notific开发者_运维百科ation that the call has ended?You need to register a PhoneStateListener on the TelephonyManager.
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_IDLE) {
// hangup
}
}
};
tm.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
To unregister your PhoneStateListener:
tm.listen(phoneListener, PhoneStateListener.LISTEN_NONE);
精彩评论