我已经看过registerReceiver必须调用它的帖子(在清单中没有定义)以接收ACTION_BATTERY_LOW意图.
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
//....
registerReceiver(new BatteryLevelReceiver(), new IntentFilter(
Intent.ACTION_BATTERY_LOW));
}
// .......
}
Run Code Online (Sandbox Code Playgroud)
广播接收器
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");
}
}
Run Code Online (Sandbox Code Playgroud)
我没有在logcat中看到"onReceive"日志语句.我正在使用模拟器来模拟电池低电量状态,使用telnet 5554和执行power capacity 10.我确实看到模拟器中的电池状态发生了变化但没有触发意图.
此外,如果我必须registerReceiver()在一个活动内部打电话而我没有打电话unregisterReceiver,onStop或者onDestroy它可以吗?如果不行,即使我的应用程序不在前台,我将如何注册接收器以接收系统意图?(除了使用清单).