• 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

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

     代码:

  • 相关阅读:
    01uni-app的创建运行在不同端上的配置 以及tarBar的配置
    js循环之map在工作中的使用
    GPTL L3-003 社交集群(并查集)
    GPLT L2-024 部落 (并查集)
    GPLT L2-010 排座位 (并查集)
    GPLT L2-007 家庭房产 (并查集)
    Codeforces Round #533 (Div. 2) D. Kilani and the Game(BFS)
    Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)
    Codeforces Round #533 (Div. 2) B. Zuhair and Strings(字符串)
    Codeforces Round #533 (Div. 2) A. Salem and Sticks(枚举)
  • 原文地址:https://www.cnblogs.com/shuanglu/p/8867341.html
Copyright © 2020-2023  润新知