Detect a screenshot Android

故事扮演 提交于 2019-11-26 20:39:43

问题


I program in recent years to Android and I wonder something : How to detect when user take a screenshot ? I want that when the user takes a screenshot , we move to the next activity . I tried the method of intercepting an event but there is a problem : when the device goes into sleep , for example, an event is intercepted . Do you have a solution for intercepts only the event of screenshot or ignore other event?

Thank you for your help !


回答1:


There is no direct Broadcast Intent to tell you that a screenshot has been taken. Some people here discuss possible options to do so ( like it's done on Snapchat ). One possible option would be to use FileObserver.




回答2:


Here is my hack to get notified when a screenshot is taken.

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);

}

Tested on Oneplus 2 and Moto E.

Update:

This finally didn't work for me and i ended up using a ContentObserver on screenshot type of entries.



来源:https://stackoverflow.com/questions/29532502/detect-a-screenshot-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!