通过向TelephonyManager注册一个listener,就可以监听手机的一些状态的变化。
自定义的监听器:
class MyPhoneStateListener extends PhoneStateListener
{
Context context;
public MyPhoneStateListener(Context con)
{
context = con;
}
public void onCallForwardingIndicatorChanged(boolean cfi)
{
}
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "call not answer", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "incoming", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "in a call", Toast.LENGTH_LONG).show();
break;
}
}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
}注册自己的监听器:
String srvcName = Context.TELEPHONY_SERVICE;
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(srvcName);
MyPhoneStateListener phoneStateListener = new MyPhoneStateListener(this);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
本文介绍如何通过向TelephonyManager注册自定义监听器来监听手机的状态变化,包括通话状态、信号强度等,并通过Toast消息展示不同状态的提示。
5608

被折叠的 条评论
为什么被折叠?



