当我尝试监视电话状态时,仅会响铃。无论何时接听,拒绝或超时,都不会看到其他两种状态,即空闲和摘机。对于一个来电,有两个振铃指示。真奇怪列出一个电话号码为02xxxxxxxxx的来电的运行日志,例如:
D / INCOMING:02xxxxxxxxx
D / oriRingtone:1
D /状态:RINGING
D / AudioManager:setStreamVolume(streamType:2,index:7,flags:6)
D / INCOMING:02xxxxxxxxx
D / oriRingtone:2
D /状态:RINGING
D / AudioManager:setStreamVolume(streamType:2,index:7,flags:6)
我添加了权限,并在BroadcastReceiver中注册了电话状态更改。
<uses-permission android:name="android.permission.READ_PHONE_STATE">
和
<receiver android:name=".PhoneCallHandler">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
我想知道为什么有两个振铃指示以及如何获取空闲和摘机状态以进行通知。谢谢。
列出信息代码:
public class PhoneCallHandler extends BroadcastReceiver {
static boolean flag = false;
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Bundle bundle = intent.getExtras();
String phoneNumber = bundle.getString("incoming_number");
Log.d("INCOMING", phoneNumber);
String state = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
//Log.d("ACTION", action);
AudioManager auManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int oriRingtone = auManager.getRingerMode();
Log.d("oriRingtone", String.valueOf(oriRingtone));
Log.d("State", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
switch (phoneNumber) {
case "02584929790":
auManager.setStreamVolume(AudioManager.STREAM_RING, 7, AudioManager.FLAG_ALLOW_RINGER_MODES | AudioManager.FLAG_PLAY_SOUND);
break;
}
}else{
auManager.setRingerMode(oriRingtone);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
BroadcastReceiver
`public class IncommingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
state = TelephonyManager.CALL_STATE_IDLE;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
}
}`
清单
<receiver android:name=".IncommingCallReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />