我有一个班级,一个电话呼叫状态的听众。在此,当呼叫断开此行时,
case TelephonyManager.CALL_STATE_IDLE:
Log.e(TAG, "CALL_STATE_IDLE");
break;
执行时间超过一次。这严重影响了我的要求。如果有人知道这个问题,请帮助我。
提前致谢
public class IncomingCallListener extends BroadcastReceiver
{
private Context mContext;
private static final String TAG = "CustomBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
mContext = context;
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
}
public class CustomPhoneStateListener extends PhoneStateListener
{
private static final String TAG = "CustomPhoneStateListener";
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
if(!incomingNumber.equalsIgnoreCase(""))
{
Log.e(TAG, "CALL_STATE_RINGING");
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e(TAG, "CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.e(TAG, "CALL_STATE_IDLE");
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
答案 0 :(得分:4)
将您的代码更改为:
public class IncomingCallListener extends BroadcastReceiver
{
private Context mContext;
private static final String TAG = "CustomBroadcastReceiver";
@Override
public void onReceive(Context context, Intent intent)
{
mContext = context;
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
}
else{
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
// DO SOME CODE HERE...
}
else{
if (state.equals(TelephonyManager.CALL_STATE_OFFHOOK))
{
// DO SOME CODE HERE...
}
}
}
}
}
//YOUR CODE HERE...
答案 1 :(得分:0)
我改变了一点....这是工作代码...
public class IncomingCallListener extends BroadcastReceiver
{
private Context mContext;
private static final String TAG = "CustomBroadcastReceiver";
TelephonyManager telephony;
CustomPhoneStateListener customPhoneListener ;
@Override
public void onReceive(Context context, Intent intent)
{
mContext = context;
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
}
}
public class CustomPhoneStateListener extends PhoneStateListener
{
private static final String TAG = "CustomPhoneStateListener";
Handler handler=new Handler();
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
if(!incomingNumber.equalsIgnoreCase(""))
{
//YOUR CODE HERE
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//YOUR CODE HERE
break;
case TelephonyManager.CALL_STATE_IDLE:
//YOUR CODE HERE
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_NONE);
}
}
}