在清单文件中声明电池广播接收器不工作? [英] Battery broadcast receiver declared in manifest file does not work?

查看:143
本文介绍了在清单文件中声明电池广播接收器不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以使系统已知的广播接收机:一是在清单文件与此元素声明。另一种是在java中code动态创建接收器。

现在,接收器已被动态创建在java中code和它的工作normally.But为什么第一种方式申报清单文件未能???

是否有任何人谁成功???

感谢。

AndroidManifest.xml中

 <接收器的Andr​​oid版本:NAME =pj.batteryinfo.BatteryReceiver>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.BATTERY_CHANGED>< /作用>
        &所述; /意图滤光器>
    < /接收器>
 

解决方案

对于某些操作,你只能申报的BroadcastReceiver在Java中code。出现这种情况的 BATTERY_CHANGED 的行动和 SCREEN_ON SCREEN_OFF 等等据我所知。

在声明中Manifest.xml文件一个BroadcastReceiver,无论你的应用程序正在运行与否,BroadcastReceiver的将被唤醒,因此,的onReceive 方法将调用。

为什么呢?我想这是因为 BATTERY_CHANGED 动作是很常见的发生,如果你可以在清单申报之时,系统会经常发送大量的广播,从而消耗电池显着;但是,当你在code声明它的的BroadcastReceiver只会活动运行时是有效的,从而避免极端的电池消耗。为了节省电池,机器人不允许这种行为的文件中注册。

这只是我的猜测。我没有看到有这方面的官方解释。作为一个开发者,我只记住这样的行动,而不是其背后的原因。

There are two ways to make a broadcast receiver known to the system: One is declare it in the manifest file with this element. The other is to create the receiver dynamically in java code.

Now, the receiver has been created dynamically in java code and it does work normally.But why the first way "Declare in manifest file" failed???

Is there anyone who success???

Thanks.

AndroidManifest.xml

        <receiver android:name="pj.batteryinfo.BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BATTERY_CHANGED"></action>
        </intent-filter>
    </receiver>

解决方案

For some actions, you could only declare the BroadcastReceiver in Java code. This happens to the BATTERY_CHANGED action and SCREEN_ON,SCREEN_OFF so far as I know.

When you declare a BroadcastReceiver in the Manifest.xml file, no matter whether your app is running or not, the BroadcastReceiver will be awaken and thus the onReceive method will be called.

Why?I think this is because the BATTERY_CHANGED action is very common to take place and if you can declare it in the Manifest, the system will often send a lot of broadcasts and thus consumes battery dramatically;however, when you declare it in the code, the broadcastReceiver will only be effective when the activity is running and thus avoid extreme battery consumption. To save battery, Android doesn't allow such actions to be registered in the file.

This is just my guess. I didn't see any official explanations on this. As a developer, I just memorize such actions, rather than the reasons behind them.

这篇关于在清单文件中声明电池广播接收器不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆