java 检测电池充电Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16177325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting Battery Charging Android
提问by OlejkaKL
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.
我以编程方式拒绝来电。“呼叫拒绝”和“发送短信”工作正常,但我只想在手机处于充电模式时拒绝呼叫。
I'm trying to implement the following code :
我正在尝试实现以下代码:
case TelephonyManager.CALL_STATE_RINGING:
if (isCharging())
{
reject();
sendSMS(incomingNumber);
}
break;
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.
但是该应用程序一直在处理来电。
Please help me to figure this out!
请帮我解决这个问题!
I'm receiving the following errors on the LogCat:
我在 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.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.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)
Thank you.
谢谢你。
回答by schwertfisch
Add the permisions in you Manifiest File
在您的清单文件中添加权限
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
</receiver>
Add this code in your class, you can set a varible flag isCharging and check is is setted. The BatteryManager broadcasts all battery and charging details in a sticky Intent that includes the charging status.
将此代码添加到您的类中,您可以设置一个变量标志 isCharging 并设置检查。BatteryManager 在包含充电状态的粘性 Intent 中广播所有电池和充电详细信息。
The charging status can change as easily as a device can be plugged in, so it's important to monitor the charging state for changes and alter your refresh rate accordingly.
充电状态可以像插入设备一样轻松更改,因此监控充电状态的变化并相应地改变刷新率非常重要。
Ref:// developer.android.com documentation.
参考://developer.android.com 文档。
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
//remove some variables if you don't need it.
}
}
回答by CommonsWare
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.
您的isCharging()
方法大概是从onReceive()
a调用的BroadcastReceiver
。在这种情况下,您不能registerReceiver()
直接调用,即使使用null
第一个参数也是如此。
Instead of:
代替:
Intent batteryStatus = contextMember.registerReceiver(null, ifilter);
try:
尝试:
Intent batteryStatus = contextMember.getApplicationContext().registerReceiver(null, ifilter);
See this blog postfor more background.
回答by StoneBird
In your activity, create a new BroadcastReceiver
instance or your class instance that extends it. Also create your IntentFilter
and the intent to be filtered. Then in your activity, override onReceive()
and add whatever you want to do when the activity receives the corresponding intent there.
在您的活动中,创建一个新BroadcastReceiver
实例或扩展它的类实例。还要创建您的IntentFilter
和要过滤的意图。然后在您的活动中,onReceive()
当活动在那里收到相应的意图时,覆盖并添加您想做的任何事情。
You can instantiate the receiver class in onResume()
and unregister it in onPause()
.
您可以在 中实例化接收器类onResume()
并在 中取消注册onPause()
。
Something like this:
像这样的东西:
public class YourAc extends Activity{
BroadcastReceiver br;
@Override
public void onResume(){
//instantiate your intent filter and other related stuff here
//register the receiver
}
@Override
public void onPause(){
//unregister your receiver
}
@Override
public void onReceive(){
//change the boolean isCharging to false
}
}