您的位置:首页 > 移动开发 > Android开发

Android 开发之PhoneState与CallState

2014-11-05 13:22 148 查看

Android  开发之PhoneState与CallState

最近在研究Phone应用,记录一下相关信息,希望对大家也会有用实现手机电话状态的监听,主要依靠两个类:TelephoneManger和PhoneStateListener。TelephonseManger提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。应用程序可以注册listener来监听电话状态的改变。我们不能对TelephonyManager进行实例化,只能通过获取服务的形式:Context.getSystemService(Context.TELEPHONY_SERVICE);当然我们这里的重点不是服务
//電話狀態的ListenerMyPhoneStateListener myPhoneStateListener = new MyPhoneStateListener();//取得TelephonyManagerTelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);//將電話狀態的Listener加到取得TelephonyManagertelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);}public class MyPhoneStateListener extends PhoneStateListener {@Overridepublic void onCallStateChanged(int state, String phoneNumber) {switch (state) {//電話狀態是閒置的case TelephonyManager.CALL_STATE_IDLE:break;//電話狀態是接起的case TelephonyManager.CALL_STATE_OFFHOOK:Toast.makeText(MainActivity.this, "正接起電話…", Toast.LENGTH_LONG).show();break;//電話狀態是響起的case TelephonyManager.CALL_STATE_RINGING:Toast.makeText(MainActivity.this, phoneNumber + "正打電話來…", Toast.LENGTH_LONG).show();break;default:break;}}

public void updatecallstate(Call call){final Call.State state = call.getState();switch (state) {case IDLE://通话空闲break;case ACTIVE://通话中break;case HOLDING://通话保持(等待回应(接听等))break;case DIALING:case ALERTING://拨号break;case INCOMING:case WAITING://来电break;case DISCONNECTING://正在挂断break;case DISCONNECTED://通话中断(网络等原因)break;}}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: