ACTION_BATTERY_LOW 未从清单中触发?

发布于 2024-10-25 10:36:49 字数 469 浏览 1 评论 0原文

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>

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

Does action_battery_low allow being fired from the manifest because I thought it did?

Here is my manifest for it:

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

But it never gets fired when I get the low battery warning from the system. Can this only be fired explicitly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

年华零落成诗 2024-11-01 10:36:49

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

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

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

不幸的是,Reto Meier 也错误地定义了深入了解位置中的操作。也已针对此问题提交了

The original question states that the receiver does not receive intents. This is because the receiver was declared as <reciever> rather than <receiver>. Had the receiver element been declared correctly, it would have worked.

Another major source of confusion is the fact that the Android documentation incorrectly references "android.intent.action.ACTION_BATTERY_LOW" and "android.intent.action.ACTION_BATTERY_OKAY". There is an existing Android Issue for this documentation error, but you should be aware that some of the comments on that issue are misleading.

Instead, the receiver's actions must be "android.intent.action.BATTERY_LOW" and "android.intent.action.BATTERY_OKAY". When referencing these actions from Java source, you may use the constants android.content.Intent.ACTION_BATTERY_LOW and android.content.Intent.ACTION_BATTERY_OKAY which are defined correctly.

Unfortunately Reto Meier also incorrectly defines the action in A Deep Dive Into Location. An issue has been filed for this as well.

二手情话 2024-11-01 10:36:49

从我自己的测试来看,我遇到了与 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_)。进行此更改后,我能够在清单中注册接收器并成功接收事件(通过模拟器的电源容量命令进行测试)

From my own testing, I ran into the same issue and solution as GeoBio Boo

But then I took a closer look at my code and noticed that I was filtering on the action android.intent.action.ACTION_BATTERY_LOW as described in the docs: https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

In reality, the action is android.intent.action.BATTERY_LOW (no ACTION_). Once I made this change, I was able to register the receiver in the Manifest and receive the event successfully (tested through emulator's power capacity command)

岛徒 2024-11-01 10:36:49

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

换句话说,在 onResume() 或 onCreate() 调用中:
registerReceiver(接收器, new IntentFilter(Intent.ACTION_BATTERY_LOW));

您不需要 BATTERY_STATS 权限。

You need to programatically register for ACTION_BATTERY_LOW. I spent a very long time trying to figure this out, and realized that registering it in the AndroidManifest.xml does not work.

In other words, in your onResume() or onCreate() call:
registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_LOW));

You do NOT need the BATTERY_STATS permission.

赤濁 2024-11-01 10:36:49

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

您可能已请求权限以捕获 BATTERY_LOW 操作。
尝试将 添加到您的清单中。

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

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

Disregard the permission suggestion in this answer, it is incorrect.

You might have request permission in order to catch the BATTERY_LOW action.
Try adding <uses-permission android:name="android.permission.BATTERY_STATS"/> to your manifest.

Also, you can place multiple actions within the same intent-filter such as:

<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>
旧瑾黎汐 2024-11-01 10:36:49

你的清单呼叫是正确的,你的接收者呢?

根据 Reto Meier 在他的传奇深入了解位置< /a>,你应该使用:

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

your manifest call is correct, what about your receiver?

according Reto Meier in his legendary Deep Dive Into Location, you should use:

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

and your receiver activity should check

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

i took it one step beyond and listen to 5 battery related events:

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

and then receive them like this (abbreviated, fill in the end)

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

must confess that i dont like the BatteryManager results. any criticism is welcome.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文