Android 实现通话监听
2010-05-19 16:16:00 来源:WEB开发网import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;
public class Telephony extends Activity {
private static final String TAG = "Telephony";
TextView view = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TelephonyManager mTelephonyMgr = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(),
PhoneStateListener.LISTEN_CALL_STATE);
view = new TextView(this);
view.setText("listen the state of phone ");
setContentView(view);
}
class TeleListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state,
String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: {
Log.e(TAG, "CALL_STATE_IDLE");
view.append("CALL_STATE_IDLE " + " ");
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
Log.e(TAG, "CALL_STATE_OFFHOOK");
view.append("CALL_STATE_OFFHOOK" + " ");
break;
}
case TelephonyManager.CALL_STATE_RINGING: {
Log.e(TAG, "CALL_STATE_RINGING");
view.append("CALL_STATE_RINGING" + " ");
break;
}
default:
break;
}
}
}
}
不要忘记在AndroidManifest.xml里面添加个permission.
< uses-permission android:name=
"android.permission.READ_PHONE_STATE" />
以上即Android监听通话的具体操作方法
更多精彩
赞助商链接