1

I'm running on android this

Command:

dumpsys activity activities | grep mFocusedApp

Output:

mFocusedApp=ActivityRecord{273535b u0 com.any.some/.app.AnyActivity t5595}

Expected:

com.any.some

I need the package name only Either by sed or another direct command


Edit (Copied from a comment to an answer; added to clarify that the resulting string shall be passed as an argument to commands.)

I need to restart the currently focused app so my idea was after grabbing the package name I will stop it by am stop and am start, example:

am stop com.any.some && am start com.any.some
6
  • If you wrap your commands and output in triple quotes it makes this much easier to read. Can you edit your question? Commented Oct 28, 2022 at 20:45
  • I tried now is it okay Commented Oct 28, 2022 at 20:55
  • 1
    You can do this with sed if you can write a regular expression for it. I'd need to see more examples of the output to see the patterns. Personally I wouldn't do it in shell or with regular expressions, I'd probably write a simply python script for it. But I can still help if I see more lines. Commented Oct 28, 2022 at 20:59
  • echo mFocusedApp=ActivityRecord{273535b u0 com.any.some/.app.AnyActivity t5595} | cut -d\ -f3 | cut -d/ -f1 yields com.any.some but like I say, more sample output would make for a more robust solution. Commented Oct 28, 2022 at 21:00
  • nearly the same as your other question: unix.stackexchange.com/q/722859/330217 Commented Oct 28, 2022 at 21:56

4 Answers 4

1

With GNU grep:

focusApp=$(
  dumpsys activity activities |
    grep -Po '^mFocusedApp=\S+\{\S+ \S+ \K[^\s/]+'
)

Standardly:

focusApp=$(
  dumpsys activity activities |
    sed -n 's/^mFocusedApp=[^ ]\{1,\}{[^ ]\{1,\} [^ ]\{1,\} \([^\s/]+\).*/\1/p'
)
0

Maybe

dumpsys activity activities | grep '^mFocusedApp=' | sed 's#.* \([^ ]*\)/.*#\1#'

The regular expression matches all non-space characters between and /.

Tested with

echo 'mFocusedApp=ActivityRecord{273535b u0 com.any.some/.app.AnyActivity t5595}' |sed 's#.* \([^ ]*\)/.*#\1#'

Output:

com.any.some

(More examples are needed to check if this fits all relevant input.)


Replying to a comment:

To use the package name for other commands you could save the output in a variable, e.g.

package=$(dumpsys activity activities | grep '^mFocusedApp=' | sed 's#.* \([^ ]*\)/.*#\1#')
am stop "$package" && am start "$package"
4
  • I need to restart the currently focused app so my idea was after grabbing the package name I will stop it by am stop and am start example am stop com.any.some && am start com.any.some Commented Oct 28, 2022 at 21:19
  • 1
    @NameF I don't understand what you want to say with this comment. This might be some background information that should go into the question. If you have additional questions, write a new question with a reference to this one and make clear what problem you need help with. From the single example in the question it is not clear if my solution or the cut command in a comment to the question will work in other cases. Commented Oct 28, 2022 at 21:24
  • 1
    Kindly delete the question as it is unclear! Commented Oct 28, 2022 at 21:29
  • @NameF The question as it was asked was clear and you got answers to that question. If you want to use the output to restart an app and don't know how to do so then just ask a new question about that after accepting an answer to the question you asked here. Commented Oct 30, 2022 at 12:53
0

You don't say WHY that should be the output but here's 1 guess at what you might want:

dumpsys activity activities| awk -F'[ /]' '/^mFocusedApp=/{print $3}'
com.any.some
2
  • Sorry I wanted to have the package name of the currently focused app but it worked for me now thanks! Commented Oct 31, 2022 at 5:07
  • Best I can tell thats what this answer gives you, assuming the expected output in your question is "the package name of the currently focused app". You're welcome. Commented Oct 31, 2022 at 14:10
-1
stdin=$(
  dumpsys activity activities |
    grep mFocusedApp |
    cut -d "{" -f2 |
    cut -d " " -f3 |
    cut -d "/" -f1
)
am force-stop "$stdin" && am start "$stdin"

phew thanks guys

3
  • That would match on mFocusedApp anywhere on any line rather than just at the start of the line and would match as a substring of longer strings so it's fragile, and it's using 3 unnecessary pipes and commands so it's inefficient. Commented Oct 30, 2022 at 13:01
  • May you enhance it please? Commented Oct 31, 2022 at 5:06
  • The enhancement to it would be to use one of the other answers. Commented Oct 31, 2022 at 14:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.