检测电池充电Android

时间:2013-04-23 19:09:30

标签: java android battery

我;我以编程方式拒绝来电。 “呼叫拒绝”和“sendSMS”工作正常, 但我只想在手机处于充电模式时拒绝接听电话。

我正在尝试实现以下代码:

  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;
}

但该应用程序不断打击来电。

请帮我解决这个问题!

我在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)

谢谢。

3 个答案:

答案 0 :(得分:7)

在Manifiest File中添加permisions

  <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>

在你的班级中添加此代码,你可以设置一个变量标志isCharging并设置check。 BatteryManager以包含充电状态的粘性Intent广播所有电池和充电细节。

充电状态可以像插入设备一样轻松更改,因此监控充电状态以进行更改并相应地更改刷新率非常重要。

参考:// 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.
    }
}

答案 1 :(得分:2)

您的isCharging()方法可能是从onReceive()的{​​{1}}调用的。在这种情况下,即使使用BroadcastReceiver第一个参数,也无法直接致电registerReceiver()

而不是:

null

尝试:

 Intent batteryStatus = contextMember.registerReceiver(null, ifilter);

有关更多背景信息,请参阅this blog post

答案 2 :(得分:1)

在您的活动中,创建一个新的BroadcastReceiver实例或扩展它的类实例。同时创建您的IntentFilter和过滤意图。然后在您的活动中,覆盖onReceive()并添加您在活动收到相应意图时要执行的操作。

您可以在onResume()中实例化接收器类,并在onPause()中取消注册。

这样的事情:

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
}
}