在Android部件越来越电量电量、部件、Android

2023-09-06 01:42:24 作者:有媳妇还找小三的人是畜生

我写了一个小工具为Android,我试图让电池电量。 我已经尝试使用

I wrote a widget for Android and I'm trying to get the battery level. I've tried using

Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

但我得到的错误: IntentReceiver组件不允许注册接收意图

but I get the error: "IntentReceiver components are not allowed to register to receive intents"

为什么呢?该ACTION_BATTERY_CHANGED是一个棘手的意图,我不注册一个接收器(在第一个参数为空)。

Why? the ACTION_BATTERY_CHANGED is a sticky intent and I don't register a receiver (null in the first parameter).

什么解决办法?

感谢。

推荐答案

hackbod给了解决方案的评论:

hackbod gave the solution at the comments:

使用getApplicationContext()。registerReciever()

" use getApplicationContext().registerReciever() "

 
精彩推荐
图片推荐