Files
Obsidian-Main/20.01. Android/ADB.md

73 lines
2.3 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
## am
### start
`am start`來打開一個activity
```
adb shell am start -S com.logitech.sentineliq/.MainActivity --es cameraId 0
```
其中`-S`是指先停止app再打開app。還有其他的命令如下
```
-D: enable debugging
-W: wait for launch to complete
--start-profiler <FILE>: start profiler and send results to <FILE>
-P <FILE>: like above, but profiling stops when app goes idle
-R: repeat the activity launch <COUNT> times. Prior to each repeat,
the top activity will be finished.
-S: force stop the target app before starting the activity
--opengl-trace: enable tracing of OpenGL functions
```
### 參考
- [Android activity manager "am" command help](https://gist.github.com/tsohr/5711945)
## pm
### list packages
列出所有安裝的apk
```bash
adb shell pm list packages
```
只列出 user 自己安裝的 apk:
```bash
adb shell "pm list packages -3"
```
## Forward
ADB forward用來把PC端收到的TCP轉到Android去這樣就可以透過USB ADB達到網路的功能。
例:
```
adb forward tcp:6100 tcp:7100
```
上述等於:
```
PC Android
http://127.0.0.1:6100 APP(port: 8080)
| |
| |
V----------> ADB(USB)----------->
```
Android端所回應的HTTP封包也會經由原路回來但是如果Android端要發一個request的話PC端就收不到了必須經由[[ADB#Reverse]]才行。
### 參考
* [Android Debug Bridge (adb)  |  Android Developers](https://developer.android.com/studio/command-line/adb#forwardports)
## Reverse
ADB reverse用來監聽Android端收到的request假設今天在Android APP上寫一個`http://127.0.0.1:8080/say/message`並希望PC端可以收到的話就可以用reverse來操作
```
adb reverse tcp:8081 tcp:8080
```
也就是說Android那邊發過來的封包會送交給PC這邊的port 8081。因此PC端還需要寫一個Server來監聽8081才行。
### 參考
* [adb命令-adb reverse的研究-有解無憂](https://www.uj5u.com/qita/277742.html)
## Logcat
- Enlarge logcat buffer to 16M: `adb logcat -G 16M`
## Get foreground activity
#android #foreground #activity
```
adb shell "dumpsys activity activities | grep ResumedActivity"
```