Unity获取手机的电量时间

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetTimeAndBattery : MonoBehaviour
{


    public Text Power;
    public Text TimePhone;
    public Text NetworkText;
    public Image NetImage;
    string _time = string.Empty;
    string _battery = string.Empty;

    void Start()
    {
        StartCoroutine("UpdateBattery");
        StartCoroutine("UpdateTime");
        StartCoroutine("UpdateNetwork");//识别手机使用的网络
        //Handheld.Vibrate();//调用手机振动。
    }
    IEnumerator UpdateBattery()
    {
        while (true)
        {
            //此处的battery是一个百分比数字,比如电量是93%,则这个数字是93  
            _battery = GetBatteryLevel().ToString();
            print("battery::::" + _battery);
            Power.text = _battery + "%";
            yield return new WaitForSeconds(300f);

        }
    }
    int GetBatteryLevel()
    {
        try
        {
            //不适用于华为
            string CapacityString = System.IO.File.ReadAllText("/sys/class/power_supply/battery/capacity");
            return int.Parse(CapacityString);
        }
        catch (Exception e)
        {
            Debug.Log("Failed to read battery power; " + e.Message);
        }
        return -1;
    }
    //更新技能时间
    IEnumerator UpdateTime()
    {
        DateTime now = DateTime.Now;
        TimePhone.text = string.Format("{0}:{1}", now.Hour, now.Minute);


        yield return new WaitForSeconds(60f - now.Second);
        while (true)
        {

            now = DateTime.Now;
            TimePhone.text = string.Format("{0}:{1},", now.Hour, now.Minute);
            yield return new WaitForSeconds(60f);
        }
    }
    //更新手机状态
    IEnumerator UpdateNetwork()
    {
        while (true)
        {
            GetNetWoker();
            yield return new WaitForSeconds(300f);
        }
    }
    void GetNetWoker()
    {
        if (Application.internetReachability == NetworkReachability.NotReachable)//网络不可用
        {
            NetImage.CrossFadeAlpha(1, 1, false);
            if (NetworkText) {
                NetworkText.enabled = false;
            }
            
            NetImage.color = Color.red;

        }
        else if (Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)//wifi在线
        {
            NetImage.CrossFadeAlpha(1, 1, false);
            if (NetworkText) {
                NetworkText.enabled = false;
            }
            
            NetImage.color = Color.white;
   
        }
        else if (Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)//4G在线
        {
            NetImage.CrossFadeAlpha(0, 1,false);
            if (NetworkText) {
                NetworkText.enabled = true;
                NetworkText.text = "4G";

            }

        }

    }
}

参考博客地址:https://blog.csdn.net/ldy597321444/article/details/78029675

使用Java接入安卓的SDK 获取手机的信息:http://www.cnblogs.com/wuzhang/p/wuzhang20170318.html

有的手机电量无法获取,所以研究新的方法中。待更新。

 代码:

猜你喜欢

转载自www.cnblogs.com/shuanglu/p/8867341.html
今日推荐