Detect a screenshot Android

后端 未结 2 1910
感动是毒
感动是毒 2020-11-30 08:15

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 08:33

    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 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.

提交回复
热议问题