我有以下代码,batteryLevel在这里显示正确的值 - 50。
public class AlarmEventService extends Service {
static String batteryLevel;
...
int level = intent.getIntExtra("level", 0);
batteryLevel = String.valueOf(level);
Log.i(APP_TAG, batteryLevel);
}
当我获得AlarmEventService.batteryLevel的值时,为什么在我的外部调用中该值为null?
public class AlarmEventReceiverWake extends BroadcastReceiver {
...
Log.i(APP_TAG, "Battery Level " + AlarmEventService.batteryLevel);
}
答案 0 :(得分:2)
根据您提供的代码,没有理由说它不能按预期工作。它可能是:
确保声明:
batteryLevel = String.valueOf(level);
实际上在
之前被称为Log.i(APP_TAG, "Battery Level " + AlarmEventService.batteryLevel);