计算设备充满电的剩余时间 android
Calculate Time left for Device to be fully Charged android
我有一个 activity,其中包含一个 TextView,它应该显示 android 设备的剩余时间,该设备已插入并充满电...
我使用 BatteryManager class 来实现这个......我从那个 class 创建了一个对象并发现有一个方法叫做 ComputeChargeTimeRemaining
唯一的问题是该方法的结果是 long 数据类型,我不知道如何将这种数据类型转换为时间值......这正是我决定寻求帮助的原因......这是我正在使用代码...
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
}) ;
}
}
让我们访问文档:
Compute an approximation for how much time (in milliseconds)
remains until the battery is fully charged. Returns -1 if no time can
be computed: either there is not enough current data to make a
decision or the battery is currently discharging.
那我们该如何应对呢?
一个TimeSpan
应该做
var timeSpan = TimeSpan.FromMilliseconds(remaining_time);
现在您拥有 TimeSpan
个赞 Hours
、Minutes
、Seconds
的特征。 TotalMinutes
等等,您还有格式说明符 Standard TimeSpan format strings:
var hours timeSpan.Hours;
var minutes = timeSpan.Minutes;
// or
var str = timespan.ToString("hh\:mm\:ss"));
我有一个 activity,其中包含一个 TextView,它应该显示 android 设备的剩余时间,该设备已插入并充满电...
我使用 BatteryManager class 来实现这个......我从那个 class 创建了一个对象并发现有一个方法叫做 ComputeChargeTimeRemaining
唯一的问题是该方法的结果是 long 数据类型,我不知道如何将这种数据类型转换为时间值......这正是我决定寻求帮助的原因......这是我正在使用代码...
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
}) ;
}
}
让我们访问文档:
Compute an approximation for how much time (in milliseconds) remains until the battery is fully charged. Returns -1 if no time can be computed: either there is not enough current data to make a decision or the battery is currently discharging.
那我们该如何应对呢?
一个TimeSpan
应该做
var timeSpan = TimeSpan.FromMilliseconds(remaining_time);
现在您拥有 TimeSpan
个赞 Hours
、Minutes
、Seconds
的特征。 TotalMinutes
等等,您还有格式说明符 Standard TimeSpan format strings:
var hours timeSpan.Hours;
var minutes = timeSpan.Minutes;
// or
var str = timespan.ToString("hh\:mm\:ss"));