Search code examples
androidbashadb

Bash ADB Calling an app with a variable


I have an app that, when called, outputs the string it was called with into a toast notification.

Like so:

am start -a android.intent.action.MAIN -e message "Example  String" -n com.rja.utility/.ShowToast

I wanted to create a for loop, where it would loop trough lines inside a .txt file and output them using the method above, like this:

for i in `cat /somedirectory/ToastQueue.txt`; do
    am start -a android.intent.action.MAIN -e message "$i" -n com.rja.utility/.ShowToast > dev/null
done

But appareantly that doesn't work, it complains that I have extra parametters.

So my question is, how could I make this work? I can't modify the app, so this should all be done trough the shell or ADB


Solution

  • So I got it working:

    for i in `cat /somedirectory/ToastQueue.txt`; do
        i="\"$i\""
        eval "am start -a android.intent.action.MAIN -e message $i -n com.rja.utility/.ShowToast > dev/null"
    done
    

    The message has to be quoted if it has spaces in it, but you can't quote it inside eval or that would exit it, so we quote it before passing it to the app.