prevent NPE
This commit is contained in:
parent
f7f4385d5d
commit
77245c027e
|
@ -738,11 +738,14 @@ public class WatchUpdaterService extends WearableListenerService implements
|
|||
|
||||
public static int getBatteryLevel(Context context) {
|
||||
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
if (level == -1 || scale == -1) {
|
||||
return 50;
|
||||
if (batteryIntent != null) {
|
||||
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
||||
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
if (level == -1 || scale == -1) {
|
||||
return 50;
|
||||
}
|
||||
return (int) (((float) level / (float) scale) * 100.0f);
|
||||
}
|
||||
return (int) (((float) level / (float) scale) * 100.0f);
|
||||
return 50;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue