ACTION_BATTERY_LOW没有从清单中被解雇?

时间:2011-03-22 15:29:49

标签: android battery

action_battery_low是否允许从清单中解雇,因为我认为它确实如此?

以下是我的表现:

<reciever android:name=".BatteryReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_LOW"/>
    </intent-filter>
</reciever>

但是当我从系统收到低电量警告时,它永远不会被解雇。这只能被明确解雇吗?

4 个答案:

答案 0 :(得分:15)

原始问题表明接收方没有收到意图。这是因为接收者被声明为<reciever>而不是<receiver>。如果接收器元素被正确声明,它就会起作用。

混淆的另一个主要原因是the Android documentation错误地引用了"android.intent.action.ACTION_BATTERY_LOW""android.intent.action.ACTION_BATTERY_OKAY"。此文档错误现有Android Issue,但您应该知道有关该问题的一些评论会产生误导。

相反,接收者的行为必须是"android.intent.action.BATTERY_LOW""android.intent.action.BATTERY_OKAY"。从Java源引用这些操作时,您可以使用正确定义的常量android.content.Intent.ACTION_BATTERY_LOWandroid.content.Intent.ACTION_BATTERY_OKAY

不幸的是,Reto Meier也错误地定义了A Deep Dive Into Location中的操作。问题has been filed也适用于此。

答案 1 :(得分:2)

从我自己的测试中,我遇到了与GeoBio Boo相同的问题和解决方案

但后来我仔细查看了我的代码并注意到我正在过滤android.intent.action.ACTION_BATTERY_LOW中的操作,如文档中所述:https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

实际上,动作是android.intent.action.BATTERY_LOW(没有ACTION_)。一旦我做了这个改变,我就能在Manifest中注册接收器并成功接收事件(通过模拟器的功率容量命令测试)

答案 2 :(得分:1)

忽略此答案中的权限建议,这是不正确的。

您可能拥有请求权限以捕获BATTERY_LOW操作。 尝试将<uses-permission android:name="android.permission.BATTERY_STATS"/>添加到您的清单。

此外,您可以在同一个intent-filter中放置多个操作,例如:

<reciever android:name=".BatteryReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.BATTERY_LOW"/>
    </intent-filter>
</reciever>

答案 3 :(得分:-1)

你的清单通话是正确的,接收器怎么样?

根据Reto Meier的传奇Deep Dive Into Location,你应该使用:

<receiver android:name=".receivers.PowerStateChangedReceiver">
   <intent-filter>
     <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
     <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
   </intent-filter>
</receiver>

并且您的接收器活动应该检查

boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);

我超越了一步并听取了5个与电池相关的事件:

    <receiver android:name=".ReceiverBatteryLevel">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
            <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            <action android:name="android.intent.action.ACTION_BATTERY_CHANGED"/>
        </intent-filter>
    </receiver>

然后像这样接收它们(缩写,填写到最后)

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;

public class ReceiverBatteryLevel extends BroadcastReceiver {
    private final String TAG = "TGbattery";

    int scale = -1;
    int level = -1;
    int voltage = -1;
    int temp = -1;

    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"battery Receiver was called now");
        String deviceUuid = "INVALID_IMEI";

        boolean batteryLow = intent.getAction().equals(Intent.ACTION_BATTERY_LOW);
        boolean batteryOK = intent.getAction().equals(Intent.ACTION_BATTERY_OKAY);
        boolean batteryPowerOn = intent.getAction().equals(Intent.ACTION_POWER_CONNECTED);
        boolean batteryPowerOff = intent.getAction().equals(Intent.ACTION_POWER_DISCONNECTED);
        boolean batteryChange = intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED);
        String intentAction = intent.getAction();

            // register SHUTDOWN event
        try {
                level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
                voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);

                Log.d(TAG,intentAction+"   batteryChange="+batteryChange+"   flagLo="+batteryLow+"  batteryOK="+batteryOK+"  batteryPowerOn="+batteryPowerOn+"  batteryPowerOff="+batteryPowerOff+"\n  level="+level+"  temp="+temp+"  scale="+scale+"  voltage="+voltage);
        } // catch etc
    }
 }

必须承认我不喜欢BatteryManager的结果。任何批评都是受欢迎的。