package com.test.mydemo3.util

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import java.util.Objects

object BatteryUtil {
    lateinit var technology : String
    var voltage: Int = 0 //瓦特
    var level: Int = 0 //电量
    var temperature: Double = 0.0 //温度
    lateinit var status : String
    lateinit var health : String
    lateinit var plugged : String


    /**
     * 获取电池容量
     * */
    /*fun getBatteryCapacity(context: Context): String{
        val mPowerProfile : Objects
        var batteryCapacity = 0.0
        val POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile"
        try {
            mPowerProfile = Class.forName(POWER_PROFILE_CLASS)
                .getConstructor(Context::class.java)
                .newInstance(context) as Objects
            batteryCapacity = Class
                .forName(POWER_PROFILE_CLASS)
                .getMethod("getBatteryCapacity")
                .invoke(mPowerProfile) as Double

        }catch (e: Exception){
            e.printStackTrace()
        }

        return batteryCapacity.toString()
    }*/

    fun ReceiverBatteryOhterInfo(context: Context){
        val intentFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
        val receiver = context.registerReceiver(null, intentFilter)
        technology = receiver?.getStringExtra("technology").toString()
        if (technology.equals("") || technology.equals(null)){
            technology = "Unknown"
        }
        voltage = receiver?.getIntExtra("voltage",0) as Int
        level = receiver.getIntExtra("level", 0) as Int
        temperature = receiver.getIntExtra("temperature", 0 ) / 10.0

    }

}