Advertisement
Sketchware

Mostra a bateria num TextView

Jan 21st, 2023
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.51 KB | None | 0 0
  1. TextView batteryView = (TextView) findViewById(R.id.batteryView);
  2.  
  3. IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  4. Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
  5.  
  6. int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
  7. int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
  8.  
  9. float batteryPct = level / (float)scale;
  10. int batteryPercentage = (int)(batteryPct*100);
  11.  
  12. batteryView.setText("Bateria: " + batteryPercentage + "%");
  13.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement