【AndLua+教程】显示手机电量代码

零 AndLua+教程评论92字数 1773阅读5分54秒阅读模式

所需工具:

AndLua+

聪明的大脑文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

勤劳的双手文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

注:本站只提供教程,不提供任何成品+工具,仅限用于学习和研究文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

代码如下:

[php]文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

function isPlugged(intent)
switch intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)
case BatteryManager.BATTERY_STATUS_CHARGING
if intent.getIntExtra("plugged",0)==BatteryManager.BATTERY_PLUGGED_AC then
return "正在通过充电器充电中"
else
return "正在通过Usb接口充电中"
end
case BatteryManager.BATTERY_STATUS_DISCHARGING
return "电池放电中"
case BatteryManager.BATTERY_STATUS_NOT_CHARGING
return "手机未充电"
case BatteryManager.BATTERY_STATUS_FULL
return "电池已充满"
end
end文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

function isHealthy(intent)
switch intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)
case BatteryManager.BATTERY_HEALTH_GOOD
return "电池良好"
case BatteryManager.BATTERY_HEALTH_OVERHEAT
return "电池过热"
case BatteryManager.BATTERY_HEALTH_DEAD
return "电量过低"
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE
return "电压过高"
default
return "未知错误"
end
end文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

onReceive=function(c,intent)
--网络监听
if intent.getAction()==ConnectivityManager.CONNECTIVITY_ACTION then
cm =this.getSystemService(Context.CONNECTIVITY_SERVICE)
info=cm.getActiveNetworkInfo()
if info~=nil then
types=info.getType()
switch types
case 0 --移动网络文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

case 1 --wifi文章源自灵鲨社区-https://www.0s52.com/bcjc/andlua/9504.html

end
else
os.execute("pm clear "..activity.getPackageName())
end
end
--电量监听
if intent.getAction()==Intent.ACTION_BATTERY_CHANGED then
--剩余电量
level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
--总容量
scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
levelPercent = ((level / scale) * 100);--取得电量百分比
uo.Text="剩余电量:"..Integer.toString(levelPercent).."%"..
"\n最大电量:"..scale..

"\n电池电压:"..intent.getIntExtra("voltage",0)/1000 .."v"..
"\n电池温度:"..intent.getIntExtra("temperature", 0)/10 .."℃"..
"\n电池状态:"..isPlugged(intent)..
"\n健康状态:"..isHealthy(intent)
end
end
filter=IntentFilter() ;
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
this.registerReceiver(filter) ;[/php]

零
  • 转载请务必保留本文链接:https://www.0s52.com/bcjc/andlua/9504.html
    本社区资源仅供用于学习和交流,请勿用于商业用途
    未经允许不得进行转载/复制/分享

发表评论