1

I'm attempting to track the deactivation of notifications within android. This I planned on doing by polling the notification dumpsys every x seconds. There for I've put the notification into a variable so I can search the $tmp variable for a text (in this case google.gm) and based on this it will set the $Gmail on true or false.

When I tested my script-setup via Cygwin terminal on the PC it worked great, however not on Android

getting the dumpsys notification into $tmp works fine, but when I test it (in the shell) on android it seems to not want to accept my * wildcards.

tmp=$(dumpsys notification)
[[ "$tmp" == *"google.gm"* ]] && Gmail=true || Gmail=false

I've been searching the web for the last two hours, but It'd kinda driving me crazy. I've done simplified tests to debug it and it really seems to be in the wildcard character

Does anybody see what I'm doing wrong?

After the new suggestions I managed to make this out of it:

tmp=$(dumpsys notification)
case $tmp in *notify_missed_call*) PRF1="1" ;; *) PRF1="0" ;; esac
case $tmp in *conv_notify*) PRF2="1" ;; *) PRF2="0" ;; esac
case $tmp in *NotYetThere*) PRF3="1" ;; *) PRF3="0" ;; esac
case $tmp in *stat_notify_calendar*) PRF4="1" ;; *) PRF4="0" ;; esac
echo $PRF1,$PRF2,$PRF3,$PRF4, > /sdcard/tmp.txt

But somehow it only works when put in 1 line with ; between them. any way to make this work multiline (easier to maintain) and optimize it?

thanks

4

1 回答 1

0

听起来您在该 Android 上没有 bash,因此请尝试改用 POSIX。

case $(dumpsys notification) in 
    *google.gm*) Gmail=true ;;
    *) Gmail=false ;;
esac

更新 鉴于您更新的答案,我看到您正在检查输出中的多个值dumpsys notification。假设这些字符串中只有一个可以出现在一行中,更好的方法是使用 shell 或 awk 逐行读取它

dumpsys notification | { 
    while read -r line; do
        case $line in
            *notify_missed_call*) prf1=1;;
            *conv_notify*) prf2=1;;
            *NotYetThere*) prf3=1;;
            *stat_notify_calendar*) prf4=1;;
        esac
    done
    echo "${prf1:-0},${prf2:-0},${prf3:-0},${prf4:-0}"
} > /sdcard/tmp.txt

使用 awk,您可以检测同一行中的多个值。

dumpsys notification | awk '
    /notify_missed_call/{missed=1}
    /conv_notify/{conv=1}
    /NotYetThere/{notyet=1}
    /stat_notify_calendar/{cal=1}
    END { print missed+0,conv+0,notyet+0,cal+0 }' > /sdcard/tmp.txt
于 2012-07-16T06:41:52.357 回答