gpt4 book ai didi

Android:ACTION_BATTERY_LOW 未在模拟器中触发。接收器在代码中注册,而不是 list

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:44 25 4
gpt4 key购买 nike

我看过一些帖子,其中提到必须调用 registerReceiver(未在 list 中定义)以接收 ACTION_BATTERY_LOW Intent 。

public class MainActivity extends Activity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
//....
registerReceiver(new BatteryLevelReceiver(), new IntentFilter(
Intent.ACTION_BATTERY_LOW));
}
// .......
}

广播接收器

public class BatteryLevelReceiver extends BroadcastReceiver
{
private static final String TAG = BatteryLevelReceiver.class.getSimpleName();

@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "onReceive");
}
}

我在 logcat 中没有看到“onReceive”日志语句。我正在使用模拟器模拟电池低电量状态,使用 telnet 5554 并执行 power capacity 10。我确实看到模拟器中的电池状态发生了变化,但没有触发 Intent 。

此外,如果我必须在 Activity 中调用 registerReceiver() 并且我不会在 onStoponDestroy< 上调用 unregisterReceiver/,可以吗?如果不行,即使我的应用程序不在前台,我将如何注册接收器以接收系统 Intent ? (除了使用 list )。

最佳答案

确保:

  1. 您正在将“充电器连接”设置为“无”
  2. 电池状态为“正在放电”

关于Android:ACTION_BATTERY_LOW 未在模拟器中触发。接收器在代码中注册,而不是 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14133435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com