TelephonyManager.CALL_STATE_RINGING调用的两倍,而一个呼叫振铃 [英] TelephonyManager.CALL_STATE_RINGING calls twice while one call ringing
问题描述
我用的后代 - 类PhoneStateListener的:
I use descendant - class of PhoneStateListener:
class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
if (incomingNumber!=null)
{
// code for incoming call handling
}
break;
}
super.onCallStateChanged(state, incomingNumber);
}
这是我的广播接收器:
class ServiceReceiver extends BroadcastReceiver {
CallStateListener phoneListener;
@Override
public void onReceive(final Context context, Intent intent) {
if (phoneListener == null) {
phoneListener = CallStateListener.getCallStateInstance(context);
TelephonyManagerSingletone.getInstance().getTelephonyManager().listen(CallStateListener.getCallStateInstance(context),
android.telephony.PhoneStateListener.LISTEN_CALL_STATE);
}
}
清单:
<receiver android:name="com.library.ServiceReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
但是当我用它这个样子,我收到我的CallStateListener重复呼叫。我该如何解决这个问题?
but when I use it like this, I receive duplicate call in my CallStateListener. How can I fix this?
推荐答案
当我想你的code,它运行10次,而不是只有一次运行!这是更好地申报 phoneListener
在这种情况下,一个静态变量。这样,你可以保证听
方法将不会被调用一次以上。检查了这一点:
When I tried your code, it run 10 times instead of running just once! It is better to declare phoneListener
as a static variable in this case. That way you can guarantee that listen
method will not be called more than once. Check this out:
public class ServiceReceiver extends BroadcastReceiver {
static CallStateListener phoneListener;
@Override
public void onReceive(final Context context, Intent intent) {
if (phoneListener == null) {
phoneListener = new CallStateListener();
TelephonyManager tm = (TelephonyManager)context.getSystemService("phone");
tm.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
另外,我已经改变了你处理 TelephonyManager
的方式 - 我无法找到 TelephonyManagerSingletone
你在code提及。
Also, I have changed the way you deal with TelephonyManager
- I couldn't find that TelephonyManagerSingletone
that you mentioned in your code.
我希望这可以帮助别人!
I hope this helps somebody!
这篇关于TelephonyManager.CALL_STATE_RINGING调用的两倍,而一个呼叫振铃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!