首先要說明的是並未實現,本文講一下本身的思路。html
java.lang.SecurityException: Permission Denial: broadcast asks to run as user -2 but is calling from user 0
須要android.permission.INTERACT_ACROSS_USERS_FULL 或者 android.permission.INTERACT_ACROSS_USERS 權限,而這個權限是system app的權限,第三方app是沒有權限申請的。java
shell@aries:/sdcard $ screenrecord --verbose --time-limit 10 /sdcard/1.mp4 Main display is 720x1280 @59.00fps (orientation=0) Configuring recorder for 720x1280 video/avc at 4.00Mbps Content area is 720x1280 at offset x=0 y=0 Time limit reached Encoder stopping; recorded 6 frames in 10 seconds Stopping encoder and muxer Executing: /system/bin/am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_ FILE -d file:///sdcard/1.mp4 Broadcasting: Intent { act=android.intent.action.MEDIA_SCANNER_SCAN_FILE dat=fil e:///sdcard/1.mp4 } Broadcast completed: result=0
既然如此看一下screenrecord源碼吧。android
frameworks\av\cmds\screenrecord\screenrecord.cpp * * Sends a broadcast to the media scanner to tell it about the new video. * * This is optional, but nice to have. */ static status_t notifyMediaScanner(const char* fileName) 果真有這樣一個函數,而後在 main 函數的末尾調用了此函數: if (err == NO_ERROR) { // Try to notify the media scanner. Not fatal if this fails. notifyMediaScanner(fileName); }
那麼,若是註釋掉 notifyMediaScanner(fileName); 這一行,從新編譯出來的 screenrecord 可執行程序在錄屏時就不會發廣播了,是否是就不用 root 權限了呢?
通過測試,是能夠的。shell
C:\Users\wy>adb logcat | findstr /I "ScreenRecord"
10-17 10:36:17.435 9839 9839 V ScreenRecord: Creating codec
10-17 10:36:17.531 9839 9839 V ScreenRecord: Creating encoder input surface
10-17 10:36:17.533 9839 9839 V ScreenRecord: Starting codec
10-17 10:36:17.618 9839 9839 V ScreenRecord: Codec prepared
10-17 10:36:17.623 9839 9839 V ScreenRecord: Calling dequeueOutputBuffer
10-17 10:36:17.873 9839 9839 V ScreenRecord: dequeueOutputBuffer returned -11
10-17 10:36:17.873 9839 9839 V ScreenRecord: Got -EAGAIN, looping
10-17 10:36:17.873 9839 9839 V ScreenRecord: Calling dequeueOutputBuffer
10-17 10:36:18.124 9839 9839 V ScreenRecord: dequeueOutputBuffer returned -11
10-17 10:36:18.124 9839 9839 V ScreenRecord: Got -EAGAIN, looping
10-17 10:36:18.124 9839 9839 V ScreenRecord: Calling dequeueOutputBufferapp
日誌顯示,在輸出 buffer 的時候一直返回錯誤,不停的重複嘗試,直到結束也沒成功錄屏一幀。ide
這就不知道什麼緣由了,還得去看代碼。函數
先丟這裏,望明白的大神指點。oop