Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

16
LINES

< > BotCompany Repo | #1004077 // androidGetBattery

JavaX fragment (include)

1  
import android.content.Intent;
2  
import android.content.IntentFilter;
3  
import android.os.BatteryManager;
4  
5  
static int androidGetBattery() {
6  
  Intent batteryIntent = androidContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
7  
  int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
8  
  int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
9  
10  
  // Error checking that probably isn't needed but I added just in case.
11  
  if(level == -1 || scale == -1) {
12  
      return -1;
13  
  }
14  
15  
  return (int) (((float)level / (float)scale) * 100.0f);
16  
}

Author comment

Began life as a copy of #1004076

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004077
Snippet name: androidGetBattery
Eternal ID of this version: #1004077/1
Text MD5: 22b1d3c494a235c9850d0fe8790b0714
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-06 11:52:59
Source code size: 601 bytes / 16 lines
Pitched / IR pitched: No / No
Views / Downloads: 537 / 544
Referenced in: [show references]