【问题标题】:ACTION_BATTERY_LOW not being fired from manifest?ACTION_BATTERY_LOW 没有从清单中触发?
【发布时间】:2015-02-17 07:51:36
【问题描述】:

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>

但是当我从系统收到低电量警告时,它永远不会被触发。这只能显式触发吗?

【问题讨论】:

  • 接收者不是接收者。希望对您有所帮助。
  • 您的问题引起了极大的困惑。首先,您应该回答@Ahsen 的评论-您的清单将如何与&lt;reciever&gt; 标签一起使用?没有这样的标签。其次,您接受的答案是错误的!请更正那些
  • @Mr_and_Mrs_D 显然是错字,这个问题已有 2 年历史了,我不会回去检查以确保我接受的所有答案都是正确的
  • 没有问题 - 只是它引起了很多混乱,因为人们引用了公认的答案并说:“请参阅 .ACTION_BATTERY_LOW 无法在清单中注册 (example)。无论如何 - 很快就会删除我的 cmets ;) - 我建议你编辑 reciever android:name 但留下 BatteryReciever 因为这可能是你的问题

标签: android battery


【解决方案1】:

原始问题指出接收者没有收到意图。这是因为接收者被声明为&lt;reciever&gt; 而不是&lt;receiver&gt;。如果接收器元素被正确声明,它就会起作用。

另一个造成混淆的主要原因是the Android documentation 错误地引用了"android.intent.action.ACTION_BATTERY_LOW""android.intent.action.ACTION_BATTERY_OKAY"。此文档错误存在一个现有的Android Issue,但您应该注意,关于该问题的一些 cmets 具有误导性。

相反,接收者的操作必须是"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

【讨论】:

  • Android 文档和这个答案都不正确,所以我要修正答案。 动作必须是 android.intent.action.BATTERY_LOW,而不是 android.intent.action.ACTION_BATTERY_LOW。在代码中,您可以使用正确定义为“android.intent.action.BATTERY_LOW”的 android.content.Intent.ACTION_BATTERY_LOW 常量。
【解决方案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 中注册接收器并成功接收事件(通过模拟器的电源容量命令测试)

【讨论】:

  • 谢谢,这确实有效,文档说明该操作可通过清单获得。但只有去掉“ACTION_”,就可以使用完整的字符串注册接收者,只是不在manifest中,很奇怪。
【解决方案3】:

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

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

您不需要BATTERY_STATS 权限。

【讨论】:

  • 如果您的 targetSdkVersion 大于或等于 26 并且在设备 oreo 或更高版本上运行,那么由于背景限制,您必须以编程方式注册和取消注册接收器。在这种情况下,简单地在清单中声明它是行不通的。
【解决方案4】:

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

您可能拥有请求权限才能捕获BATTERY_LOW 操作。 尝试将&lt;uses-permission android:name="android.permission.BATTERY_STATS"/&gt; 添加到您的清单中。

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

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

【讨论】:

  • 您可以尝试通过服务注册它,例如:BatteryReceiver receiver = new BatteryReceiver(); IntentFilter inf = new IntentFilter(); inf.addAction("android.intent.action.BATTERY_LOW"); registerReceiver(receiver, inf);
  • reciever ?你试过了吗?也不需要android.permission.BATTERY_STATS
【解决方案5】:

你的清单调用是正确的,你的接收器呢?

根据 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 的结果。欢迎任何批评。

【讨论】:

  • 您的代码 sn-p 缺少 } 并且无法编译 - 修改它
猜你喜欢
  • 1970-01-01
  • 2014-12-26
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2012-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多