Android无障碍服务实现应用锁+入侵拍照取证,完整开源项目实战 📅 发布时间:2026/9/11 18:54:48 👁 浏览次数: Android无障碍服务实现应用锁入侵拍照取证完整开源项目实战一、引言市面上的手机安全工具要么收费要么不透明。我写了一个完全开源MIT的 Android 安全防护工具——PhoneGuard手机防火墙 本文拆解核心实现。开源地址 https://github.com/hnpdsyn/PhoneGuard1二、核心技术实现1. 无障碍服务实现应用锁使用 AccessibilityService 监听 TYPE_WINDOW_STATE_CHANGED 事件获取当前顶层应用包名与锁定列表比对。xml123456canRetrieveWindowContent 设为 false只获取包名降低权限要求。2. 旧 Camera API 做后台拍照取证为什么不用 Camera2/CameraX 它们在后台 Service 中需要 Surface/PreviewView不如旧 API 灵活。kotlin1234567891011121314151617181920212223242526private suspend fun captureIntruderPhoto() { withContext(Dispatchers.IO) { var camera: Camera? null try { val cameraIndex findFrontCameraIndex() if (cameraIndex 0) returnwithContext camera Camera.open(cameraIndex) // 虚拟预览——无需 UI 界面 val surfaceTexture SurfaceTexture(0) camera.setPreviewTexture(surfaceTexture) camera.startPreview() delay(500) // 拍照保存 val latch CountDownLatch(1) camera.takePicture(null, null, Camera.PictureCallback { data, _ - FileOutputStream(photoFile).use { it.write(data) } latch.countDown() }) latch.await(8, TimeUnit.SECONDS) } finally { camera?.stopPreview() camera?.release() } }}关键 SurfaceTexture 做虚拟预览CountDownLatch 同步等待拍照后必须 release()。3. 入侵检测逻辑监听 SCREEN_OFF/USER_PRESENT 广播组合判断锁屏失败次数。屏幕亮起超过 3 秒未收到 USER_PRESENT 视为一次失败累计达到阈值默认 5 次触发拍照 → 获取 GPS → 播放警报 → 发送通知。4. ADB 状态检测每 5 秒读取 Settings.Global.ADB_ENABLED检测到 ADB 开启自动拍照取证高优先级通知。kotlin123456private fun isAdbEnabled(): Boolean { return try { Settings.Secure.getInt(contentResolver, Settings.Global.ADB_ENABLED) 1 } catch (e: Exception) { false }}5. 配置加密存储主密码用 EncryptedSharedPreferencesAES256-GCM加密存储避免 root 手机明文泄漏。kotlin12345678private val masterKey MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build()private val securePrefs EncryptedSharedPreferences.create( context, “phone_guard_secure_prefs”, masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM)6. 国产 ROM 后台保活原生 START_STICKY 在国产 ROM 上几乎不管用。应对策略前台服务 Boot 自启 引导用户设置自启动/忽略电池优化/后台锁定 WorkManager 兜底。三、技术栈表格维度 选型语言 Kotlin 1.9UI Jetpack Compose 1.5.4 Material 3相机 旧 Camera API后台兼容性最优存储 EncryptedSharedPreferences最低支持 Android 9 (API 28)四、开源地址GitHub https://github.com/hnpdsyn/PhoneGuard1MIT 协议欢迎 Star、fork、提 issue如果你对某个技术细节感兴趣欢迎评论区留言讨论。