当我的应用进入后台时,我如何防止Android截屏?

时间:2021-11-22 07:26:40

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.

我打算把这个功能放到应用程序类的onPause方法中,但是首先我需要知道如何实现这个功能。

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

有人知道怎么解决这个问题吗?

3 个解决方案

#1


218  

Try FLAG_SECURE:

试试FLAG_SECURE:

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

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

    setContentView(R.layout.main);
  }
}

This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. It also secures against screen recording (e.g., apps using the media projection APIs).

这绝对可以防止人工屏幕截图和ICS recent-tasks历史中的自动屏幕截图。它还可以防止屏幕录制(例如,使用媒体投影api的应用程序)。

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.

更新:它还可以在Tap点上保护,或者在Android 6.0上保护其他助手;如果用户打开这个助手,他们将无法访问UI中小部件和容器的细节。

UPDATE #2: however, not everything in the activity will be protected. Any pop-up windows — Dialog, Spinner, AutoCompleteTextView, action bar overflow, etc. — will be insecure. You can fix the Dialog problem by calling getWindow() on it and setting FLAG_SECURE. The rest... gets tricky. See this blog post for more.

更新#2:但是,并不是所有的活动都将被保护。任何弹出窗口-对话框,旋转器,AutoCompleteTextView,动作栏溢出,等等-将是不安全的。可以通过调用getWindow()并设置FLAG_SECURE来修复这个对话框问题。其余的……变得棘手。更多信息请参见这篇博文。

#2


69  

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:

使用WindowManager.LayoutParams时要小心。FLAG_SECURE,在某些设备上(在三星Galaxy ACE上得到验证,例如GT-S5830),这将使视图变得混乱。看起来像是三星特有的漏洞。我建议以下几点:

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 what a scrambled screen looks like:

这就是炒屏的样子:

当我的应用进入后台时,我如何防止Android截屏?

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

不过,这在ICS三星手机上运行良好,因此我假设问题是与姜饼设备(或更老的设备)隔离的。

#3


14  

The solution provided by CommonsWare continues to be valid also in Lollipop.

CommonsWare提供的解决方案在棒棒糖中仍然有效。

Just a note, if you want to continue to not see snapshots in recent list for the entire app, ALL the implemented activities should specify in the onCreate() method the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); before setContentView();

注意,如果您想继续在最近的列表中看到整个应用程序的快照,那么所有实现的活动都应该在onCreate()方法中指定标记getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);之前setContentView();

Otherwise a snapshot in the recent list will show the first activity without the flag if the user navigated through it.

否则,最近列表中的快照将显示第一个没有标志的活动,如果用户浏览它的话。

#1


218  

Try FLAG_SECURE:

试试FLAG_SECURE:

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

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

    setContentView(R.layout.main);
  }
}

This definitely secures against manual screenshots and automatic screenshots from the ICS recent-tasks history. It also secures against screen recording (e.g., apps using the media projection APIs).

这绝对可以防止人工屏幕截图和ICS recent-tasks历史中的自动屏幕截图。它还可以防止屏幕录制(例如,使用媒体投影api的应用程序)。

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.

更新:它还可以在Tap点上保护,或者在Android 6.0上保护其他助手;如果用户打开这个助手,他们将无法访问UI中小部件和容器的细节。

UPDATE #2: however, not everything in the activity will be protected. Any pop-up windows — Dialog, Spinner, AutoCompleteTextView, action bar overflow, etc. — will be insecure. You can fix the Dialog problem by calling getWindow() on it and setting FLAG_SECURE. The rest... gets tricky. See this blog post for more.

更新#2:但是,并不是所有的活动都将被保护。任何弹出窗口-对话框,旋转器,AutoCompleteTextView,动作栏溢出,等等-将是不安全的。可以通过调用getWindow()并设置FLAG_SECURE来修复这个对话框问题。其余的……变得棘手。更多信息请参见这篇博文。

#2


69  

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:

使用WindowManager.LayoutParams时要小心。FLAG_SECURE,在某些设备上(在三星Galaxy ACE上得到验证,例如GT-S5830),这将使视图变得混乱。看起来像是三星特有的漏洞。我建议以下几点:

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 what a scrambled screen looks like:

这就是炒屏的样子:

当我的应用进入后台时,我如何防止Android截屏?

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

不过,这在ICS三星手机上运行良好,因此我假设问题是与姜饼设备(或更老的设备)隔离的。

#3


14  

The solution provided by CommonsWare continues to be valid also in Lollipop.

CommonsWare提供的解决方案在棒棒糖中仍然有效。

Just a note, if you want to continue to not see snapshots in recent list for the entire app, ALL the implemented activities should specify in the onCreate() method the flag getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); before setContentView();

注意,如果您想继续在最近的列表中看到整个应用程序的快照,那么所有实现的活动都应该在onCreate()方法中指定标记getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);之前setContentView();

Otherwise a snapshot in the recent list will show the first activity without the flag if the user navigated through it.

否则,最近列表中的快照将显示第一个没有标志的活动,如果用户浏览它的话。