본문 바로가기

StackOverflow

[Android] 스크린 캡쳐를 막는 방법

http://stackoverflow.com/questions/9822076/how-do-i-prevent-android-taking-a-screenshot-when-my-app-goes-to-the-background


Q: 제 앱이 백그라운드로 내려갔을 때, 안드로이드가 스크린샷을 찍는 것을 막으려면 어떻게 해야 되나요?

제가 만들고 있는 앱은 보안 이슈 때문에, 앱이 백그라운드일 때 OS에서 스크린샷을 찍는 것을 막아야 합니다. 그래서 다른 앱이 뜰 때는, 제 앱의 마지막 화면이 보이지 않아야 합니다.

지금 계획중인 방법은 onPause 메소드 안에서 뭔가 동작을 시켜주면 되지 않을까 싶은데, 일단 그 전에, 애초에 스크린샷 자체를 막을 수 있는 방법을 찾고 있습니다.

혹시 이렇게 구현하는 방법을 알고 계신 분 없나요?

(질문자: StingRay5)


A: FLAG_SECURE를 한 번 해보세요.

public class FlagSecureTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(LayoutParams.FLAG_SECURE,
                         LayoutParams.FLAG_SECURE);

    setContentView(R.layout.main);
  }
}

이 방법은 ICS의 "최근 앱 목록" 에서도 화면표시를 막을 수 있습니다.

추가: 이 방법은 Android 6.0의 Now On Tap 기능에서도 마찬가지로 안전합니다. (역주: Now On Tap은 해당 앱에 대해 구글검색을 하는 기능입니다) 이렇게 하면 Now On Tap 기능이 앱의 상세내용에 접근할 수 없게 됩니다.

(답변자: CommonsWare )


A: FLAG_SECURE 를 이용할 때는 주의할 점이 있습니다. 몇몇 기기에서 동작을 안한다는 건데요, 삼성 갤럭시 ACE (GT-S5840) 에서는 화면전체가 깨져서 나옵니다. 삼성 폰에서만 나오는 에러 같네요.

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}


화면이 아래와 같이 깨져서 나옵니다.

How a scrambled screen looks like

삼성 ICS 폰에서는 정상동작하고, 진저브레드 이하 버전에서는 이상이 있는 것으로 보입니다.


(답변자: gardarh)



_

The app I'm currently building has the requirement that the app has to prevent the OS to take a screenshot of the app when it's being pushed into the background for security reasons. This way it won't be able to see the last active screen when switching between apps.

I'm planning to put this functionality in the application class's onPause method, but first I need to find out how I can achieve this functionality.

So is there anybody out there, that has a clue how to fix this?

shareeditflag

_

117down voteaccepted

Try FLAG_SECURE:

public class FlagSecureTestActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(LayoutParams.FLAG_SECURE,
                         LayoutParams.FLAG_SECURE);

    setContentView(R.layout.main);
  }
}

This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history.

UPDATE: it also secures against Now On Tap or other assistants on Android 6.0; they will not get access to the details of widgets and containers in your UI if the user brings up the assistant.

shareeditflag




_

Be careful about using WindowManager.LayoutParams.FLAG_SECURE, on some devices (verified on Samsung Galaxy ACE, e.g. GT-S5830) this will make the view scrambled. Looks like a Samsung specific bug. I recommend the following:

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}

This is how a scrambled screen looks like:

How a scrambled screen looks like

This is working properly on ICS Samsung phones though, so I'm assuming problem is isolated to Gingerbread devices (or older).

shareeditflag