【问题标题】:How to catch screen capture event on Android devices?如何在 Android 设备上捕获屏幕捕获事件?
【发布时间】:2015-11-21 12:22:12
【问题描述】:

在我的 Android 应用中,我需要检测屏幕捕获事件。我该怎么做?

【问题讨论】:

  • 我没找到答案 *-->最好先搜索再找到。我个人推荐谷歌的搜索引擎,我曾经用它毫不费力地找到这个问题的许多重复项(真的只是复制/粘贴标题)。
  • @2Dee 在堆栈溢出中创建新问题之前,我尝试使用 Google。在这个网站上我发现了同样的问题,但没有答案。

标签: android events detect screen-capture


【解决方案1】:

我在我的应用程序中使用了这段代码,它运行良好。它会在截取屏幕截图时发出通知。 我已经在 OnePlus 1、Nexus 6P、Moto G3 上对其进行了测试。在任何设备上都没有问题。

public void detectScreenShotService(final Activity activity) {

    final Handler h = new Handler();
    final int delay = 3000; //milliseconds
    final ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);

    h.postDelayed(new Runnable() {
        public void run() {

            List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(200);

            for (ActivityManager.RunningServiceInfo ar : rs) {
                if (ar.process.equals("com.android.systemui:screenshot")) {
                    Toast.makeText(activity, "Screenshot captured!!", Toast.LENGTH_LONG).show();
                }
            }
            h.postDelayed(this, delay);
        }
    }, delay);
}

【讨论】:

  • 它会影响性能,因为所有操作都在主线程上执行。为避免它使用 HandlerThread 之类的东西在后台执行 (stackoverflow.com/a/29389627/5222184)
  • 根据this SO answer 的说法,该代码只工作了大约1/4 的时间,并为同一事件生成了多次调用。
猜你喜欢
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多