16.04中没有低电量弹出窗口通知


10

我在16.04中使用Unity。由于某种原因,我没有收到有关电量不足的弹出式通知。我必须依靠顶部面板中的电池图标来查看电池是否在“低电量”侧。16.04中的默认行为是吗?还是我不会因为电量不足而弹出窗口?


你可以尝试重新安装indicator-power 包。如果你愿意,我还可以提供一个脚本,可以给你通知。
谢尔盖Kolodyazhnyy

感谢@Serg,请给我同样的命令。
user227495 '16

好的,我会在几分钟后发布答案。
Sergiy Kolodyazhnyy

我帖子下的评论已移至聊天。我们可以在那里继续进行故障排除。
Sergiy Kolodyazhnyy

@SergiyKolodyazhnyy这是笔记本电池电量低还是主板电池低。讨论的解决方案是否还参考通常用于保持时钟寿命的主板的cmos电池?
simgineer

Answers:


5

尝试indicator-power使用以下命令重新安装:

sudo apt-get install --reinstall indicator-power

如果那不能解决问题,请考虑使用我以前的回答之一提供的电池监视脚本:https : //askubuntu.com/a/603322/295286

以下是python脚本,可以在电池电量超过一定百分比时通知您,并在电池电量达到10%之后挂起系统。用法很简单:

python battery_monitor.py INT

其中INT是所需电池百分比的整数值,例如30

您还可以将以上命令添加到启动应用程序,以在每次登录Unity会话时启动此脚本。

源代码

根据聊天和评论中的OP请求,脚本现在带有两个参数,第一个用于放电通知,第二个用于充电通知。

也可用作Github Gitst

#!/usr/bin/env python
from gi.repository import Notify
import subprocess
from time import sleep, time
from sys import argv
import dbus


def send_notification(title, text):
    try:
        if Notify.init(argv[0]):
            n = Notify.Notification.new("Notify")
            n.update(title, text)
            n.set_urgency(2)
            if not n.show():
                raise SyntaxError("sending notification failed!")
        else:
            raise SyntaxError("can't initialize notification!")
    except SyntaxError as error:
        print(error)
        if error == "sending notification failed!":
            Notify.uninit()
    else:
        Notify.uninit()


def run_cmd(cmdlist):
    try:
        stdout = subprocess.check_output(cmdlist)
    except subprocess.CalledProcessError:
        pass
    else:
        if stdout:
            return stdout


def run_dbus_method(bus_type, obj, path, interface, method, arg):
    if bus_type == "session":
        bus = dbus.SessionBus()
    if bus_type == "system":
        bus = dbus.SystemBus()
    proxy = bus.get_object(obj, path)
    method = proxy.get_dbus_method(method, interface)
    if arg:
        return method(arg)
    else:
        return method()


def suspend_system():
    run_dbus_method('session',
                    'com.canonical.Unity',
                    '/com/canonical/Unity/Session',
                    'com.canonical.Unity.Session',
                    'Suspend', 'None')


def get_battery_percentage():
    output = run_cmd(['upower', '--dump']).decode().split('\n')
    found_battery = False
    for line in output:
        if 'BAT' in line:
            found_battery = True
        if found_battery and 'percentage' in line:
            return line.split()[1].split('%')[0]


def main():
    end = time()
    battery_path = ""
    for line in run_cmd(['upower', '-e']).decode().split('\n'):
        if 'battery_BAT' in line:
            battery_path = line
            break
    while True:
        notified = False
        while subprocess.call(['on_ac_power']) == 0:

            sleep(0.25)
            run_dbus_method('system', 'org.freedesktop.UPower',
                            battery_path, 'org.freedesktop.UPower.Device',
                            'Refresh', 'None')
            battery_percentage = int(get_battery_percentage())
            if battery_percentage == int(argv[2]) and not notified:
               subprocess.call( ['zenity', '--info','--text', 'Battery reached' + argv[2] + '%'  ]  ) 
               notified = True
        while subprocess.call(['on_ac_power']) == 1:

            sleep(0.25)
            run_dbus_method('system', 'org.freedesktop.UPower',
                            battery_path, 'org.freedesktop.UPower.Device',
                            'Refresh', 'None')
            battery_percentage = int(get_battery_percentage())

            if battery_percentage <= int(argv[1]):
                if battery_percentage <= 10:
                    send_notification('Low Battery',
                                      'Will suspend in 60 seconds')
                    sleep(60)
                    suspend_system()
                    continue
                if end < time():
                    end = time() + 600
                    send_notification('Low Battery', 'Plug in your charger')

if __name__ == '__main__':
    main()

评论不用于扩展讨论。此对话已转移至聊天

9

这是不正常的,我正在运行16.04并出现弹出窗口,但我使用的是gnome shell tho。

您可以创建一个脚本来向您发送消息。

battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ $battery_level -le 10 ]
then
    notify-send "Battery low" "Battery level is ${battery_level}%!"
fi

然后做一个cron作业,每隔几分钟运行一次。


谢谢@ArneN。虽然我不知道如何经营玉米工作。另外,有什么方法可以配置核心文件,以便我们可以跳过脚本?
user227495 '16

要进行cron作业crontab -e,请按2并选择nano编辑器(仅当您从未进行过cron作业时),以打开终端类型,然后打开文件,向下滚动至底部并添加新行。/2 * * * * my-script.shctrl + x,然后键入y并输入。那应该工作。不知道核心文件对不起。
Cyber​​_Star '16

会做。我仍在逐一尝试。希望通过核心文件修复它。
user227495

谢谢!我在startupapps上放了这个命令bash -c 'while true;do n="$(acpi -b |egrep "[[:digit:]]*%" -o |tr -d "%")";declare -p n;if((n<30));then notify-send "Low battery warning!" "$n%";fi;sleep $((5*60));done'
VeganEye

3

是的,这很正常。我写了一个简单的bash脚本来设置电池通知。

#!/usr/bin/env bash
# check if acpi is installed.
if [ `dpkg -l | grep acpi | grep -v acpi-support | grep -v acpid | grep -c acpi` -ne 1 ]; then
    echo "run 'sudo apt install acpi' then run '$0' again."
    exit
fi

if [ $# -eq 1 ] && [ "$1" == "--install" ]; then
    echo "installing battery notifier..."

    if [ ! -e "$HOME/bin" ]; then
        mkdir $HOME/bin
    fi  

    cp $0 $HOME/bin/bn.sh
    (crontab -l 2>/dev/null; echo "*/2 * * * * $HOME/bin/bn.sh") | crontab -

else
    # check if power adapter is plugged in, if not, check battery status.
    if [ -z "`acpi -a | grep on-line`" ]; then
        batlvl=`acpi -b | grep -P -o '[0-9]+(?=%)'`

        if [ $batlvl -le 15 ] && [ $batlvl -ge 11 ]; then
            notify-send "Battery is at $batlvl%. Please plug your computer in."
        elif [ $batlvl -le 10 ] && [ $batlvl -ge 6 ]; then
            notify-send "Battery is at $batlvl%. Computer will shutdown at 5%."
        elif [ $batlvl -le 5 ]; then
            notify-send "BATTERY CRITICALLY LOW, SHUTTING DOWN IN 3 SECONDS!"
            sleep 3
            shutdown -h now
        fi
    fi  
fi

我的github帐户上也有此说明。希望这对您有所帮助,并使您更轻松。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.