1234567891011121314151617181920212223 |
- package com.example.androidbatteryplugin;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.BatteryManager;
- import com.unity3d.player.UnityPlayer;
- import com.unity3d.player.UnityPlayerActivity;
- public class BatteryPlugin extends UnityPlayerActivity {
- public static int GetBatteryLevel() {
- IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- Intent batteryStatus = UnityPlayer.currentActivity.registerReceiver(null, ifilter);
- int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
- int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
- float batteryPct = (level / (float) scale) * 100;
- return (int)batteryPct;
- }
- }
|