2

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

5 回答 5

16

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

另一个主要的混淆来源是Android 文档错误地引用"android.intent.action.ACTION_BATTERY_LOW""android.intent.action.ACTION_BATTERY_OKAY". 此文档错误存在一个现有的Android 问题,但您应该注意,关于该问题的某些评论具有误导性。

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

不幸的是,Reto Meier 也错误地定义了A Deep Dive Into Location中的动作。也为此提出了一个问题。

于 2013-09-22T16:50:26.473 回答
2

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

但后来我仔细查看了我的代码,发现我正在过滤操作 android.intent.action.ACTION_BATTERY_LOW,如文档中所述:https ://developer.android.com/training/monitoring-device-state/电池监控.html

实际上,动作是 android.intent.action.BATTERY_LOW(没有 ACTION_)。进行此更改后,我就可以在 Manifest 中注册接收器并成功接收事件(通过模拟器的电源容量命令测试)

于 2013-05-14T19:17:30.613 回答
1

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

您可能需要请求权限才能捕获该BATTERY_LOW操作。尝试添加<uses-permission android:name="android.permission.BATTERY_STATS"/>到您的清单中。

此外,您可以在同一个意图过滤器中放置多个操作,例如:

<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>
于 2011-03-22T15:38:10.763 回答
1

您需要以编程方式注册ACTION_BATTERY_LOW. 我花了很长时间试图弄清楚这一点,并意识到在其中注册它AndroidManifest.xml不起作用。

换句话说,在您的 onResume() 或 onCreate() 调用中: registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));

您不需要BATTERY_STATS许可。

于 2012-09-10T09:25:07.577 回答
-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 结果。欢迎任何批评。

于 2013-04-09T17:31:52.077 回答