【问题标题】:Calculate Time left for Device to be fully Charged android计算设备充满电的剩余时间
【发布时间】:2021-01-11 07:17:36
【问题描述】:

我有一个包含 TextView 的活动,它应该显示插入的 android 设备充满电的剩余时间... 我使用 BatteryManager 类来实现这一点...我从该类创建了一个对象并发现有一个名为 ComputeChargeTimeRemaining 的方法 唯一的问题是该方法的结果是长数据类型,我不知道如何将此数据类型转换为时间值...这正是我决定寻求帮助的原因...这里是正在使用的代码...

class Notes : AppCompatActivity{
//TextView to show remaining charge time
TextView mytext;
//Timer to update the value of the TextView every Second 
System.Timers.Timer _timer;
protected override void OnCreate(Bundle SavedInstanceState){
  mytext=(TextView)FindViewById(Resource.Id.textView1);
_timer=new System.Timers.Timer(1000);
_timer.Enabled =true;
_timer.Elapsed += Timer_Elapsed;
  } 
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
RunOnUiThread(()=>{
BatteryManager batteryManager=(BatteryManager)GetSystemService(Context.BatteryService);
long remaining_time=batteryManager.ComputeRemainingChargeTime();
//Here is where I have a problem converting the long to real hours,minutes time format, please help
}) ;
} 
} 

【问题讨论】:

    标签: c# android battery


    【解决方案1】:

    让我们访问文档:

    computeChargeTimeRemaining

    计算多少时间的近似值(以毫秒为单位) 直到电池充满电。如果没有时间,则返回 -1 被计算:要么没有足够的当前数据来制作 决定或电池当前正在放电。

    那么我们该如何应对呢?

    TimeSpan 应该这样做

    var timeSpan = TimeSpan.FromMilliseconds(remaining_time);
    

    现在你有TimeSpan喜欢HoursMinutesSeconds的功能。 TotalMinutes 等,你也有格式说明符Standard TimeSpan format strings:

        var hours timeSpan.Hours;
        var minutes = timeSpan.Minutes;
        // or
        var str = timespan.ToString("hh\\:mm\\:ss"));
    

    【讨论】:

    • 请发布将显示分钟和小时的整个解决方案...
    • 如何检测充电器是否连接
    • @CoderTimothy 这是一个不同的问题。如果你问另一个问题,一定会有人回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多