prevent NPE

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

View file

@ -738,6 +738,7 @@ 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));
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (level == -1 || scale == -1) { if (level == -1 || scale == -1) {
@ -745,4 +746,6 @@ public class WatchUpdaterService extends WearableListenerService implements
} }
return (int) (((float) level / (float) scale) * 100.0f); return (int) (((float) level / (float) scale) * 100.0f);
} }
return 50;
}
} }