prevent NPE

This commit is contained in:
Milos Kozak 2018-06-26 10:45:56 +02:00
parent f7f4385d5d
commit 77245c027e

View file

@ -738,11 +738,14 @@ public class WatchUpdaterService extends WearableListenerService implements
public static int getBatteryLevel(Context context) { public static int getBatteryLevel(Context context) {
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); if (batteryIntent != null) {
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if (level == -1 || scale == -1) { int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
return 50; 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;
} }
} }