我以编程方式拒绝来电。“呼叫拒绝”和“sendSMS”工作正常,但我只想在手机处于充电模式时拒绝呼叫。
我正在尝试实现以下代码:
case TelephonyManager.CALL_STATE_RINGING:
if (isCharging())
{
reject();
sendSMS(incomingNumber);
}
break;
正在充电:
public boolean isCharging()
{
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = contextMember.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean bCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
return bCharging;
}
但是该应用程序一直在处理来电。
请帮我解决这个问题!
我在 LogCat 上收到以下错误:
E/AndroidRuntime(11160): FATAL EXCEPTION: main
E/AndroidRuntime(11160): android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
E/AndroidRuntime(11160): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:163)
E/AndroidRuntime(11160): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:157)
E/AndroidRuntime(11160): at com.SmartDialer_app.SmartDialer.MyPhoneStateListener.isCharging(MyPhoneStateListener.java:106)
E/AndroidRuntime(11160): at com.SmartDialer_app.SmartDialer.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListene r.java:57)
E/AndroidRuntime(11160): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
E/AndroidRuntime(11160): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(11160): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(11160): at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(11160): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11160): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(11160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(11160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(11160): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(11160): FATAL EXCEPTION: main
E/AndroidRuntime(11160): android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents
E/AndroidRuntime(11160): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:163)
E/AndroidRuntime(11160): at android.app.ReceiverRestrictedContext.registerReceiver(ContextImpl.java:157)
E/AndroidRuntime(11160): at com.SmartDialer_app.SmartDialer.MyPhoneStateListener.isCharging(MyPhoneStateListener.java:1 06)
E/AndroidRuntime(11160): at com.SmartDialer_app.SmartDialer.MyPhoneStateListener.onCallStateChanged(MyPhoneStateListener.java:57)
E/AndroidRuntime(11160): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
E/AndroidRuntime(11160): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(11160): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(11160): at android.app.ActivityThread.main(ActivityThread.java:4898)
E/AndroidRuntime(11160): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11160): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(11160): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
E/AndroidRuntime(11160): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
E/AndroidRuntime(11160): at dalvik.system.NativeStart.main(Native Method)
谢谢你。