首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >16.04无低电量弹出通知

16.04无低电量弹出通知
EN

Ask Ubuntu用户
提问于 2016-07-20 13:32:45
回答 2查看 13.2K关注 0票数 12

我在16.04中使用了统一号。出于某种原因,我没有收到关于低电池的弹出通知。我必须依靠电池图标在顶部面板,看看电池是否在“低电池”方面。默认行为在16.04中吗?还是我不会因为电池少而弹出弹出?

EN

回答 2

Ask Ubuntu用户

回答已采纳

发布于 2016-07-20 13:58:52

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

代码语言:javascript
复制
sudo apt-get install --reinstall indicator-power

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

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

代码语言:javascript
复制
python battery_monitor.py INT

其中INT是所需电池百分比的整数值,应在此值接收通知,例如30

您还可以将上面的命令添加到启动应用程序中,以便在每次登录到联合会话时启动此脚本

源代码

根据聊天和评论中的OP请求,该脚本现在采用两个参数,一个用于释放通知,另一个用于收费通知。

也可作为Github提供

代码语言:javascript
复制
#!/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()
票数 7
EN

Ask Ubuntu用户

发布于 2016-07-25 18:07:26

我找到了一个很好的应用程序,它可以实现低电池、全电池等功能。

读这个

http://www.omgubuntu.co.uk/2016/07/ubuntu-battery-monitor-notifications

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/800875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档