-
-
Save codeforfun-jp/9d2411df00ac44ddc7dc76a0f479b605 to your computer and use it in GitHub Desktop.
Android Studio Battery Checker 2-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BatteryReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
/* 省略 */ | |
if (action != null && action.equals(Intent.ACTION_BATTERY_CHANGED)) { | |
/* 省略 */ | |
// バッテリー残量 | |
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); | |
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); | |
int percentage = level * 100 / scale; | |
percentageLabel.setText(context.getString(R.string.percentage_label, percentage)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment