检测电池充电的Android [英] Detecting Battery Charging Android
问题描述
我,M编程拒绝接听电话。 呼叫拒绝和sendSMS做工精细,
但我想拒绝呼叫只有当手机处于充电模式。
I;m programmatically rejecting an incoming calls. The "call reject" and "sendSMS" work fine, but I want to reject the call only if the phone is in charging mode.
我想实现以下code:
I'm trying to implement the following code :
case TelephonyManager.CALL_STATE_RINGING:
if (isCharging())
{
reject();
sendSMS(incomingNumber);
}
break;
isCharging:
isCharging:
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;
}
但是,应用程序可以使破碎的来电。
But the app keeps crushing on incoming call.
请帮我算出这个!
我收到的LogCat中以下错误:
I'm receiving the following errors on the 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)
感谢您。
推荐答案
您 isCharging()
方法presumably正在从名为的onReceive ()
的广播接收器
。在这种情况下,你不能叫 registerReceiver()
直接,即使有空
第一个参数。
Your isCharging()
method is presumably being called from onReceive()
of a BroadcastReceiver
. In that case, you cannot call registerReceiver()
directly, even with a null
first parameter.
而不是:
Intent batteryStatus = contextMember.registerReceiver(null, ifilter);
尝试:
Intent batteryStatus = contextMember.getApplicationContext().registerReceiver(null, ifilter);
请参阅这个博客帖子了解背景。
这篇关于检测电池充电的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!