代码之家  ›  专栏  ›  技术社区  ›  akhilsreekar

防止Android One设备中的屏幕捕获

  •  0
  • akhilsreekar  · 技术社区  · 3 年前

    我想防止在android手机中捕获屏幕截图。我添加了行

    requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)

    这在正常设备中运行良好。但当我尝试安卓一号设备时( https://www.android.com/intl/en_in/one/ ),它仍在捕捉屏幕截图。

    我在安卓设备上尝试了其他应用程序,如AmazonPrime、Hotstar和GooglePay。。。尽管他们正在捕捉屏幕截图,但内容完全是黑色的。如何实现这一点或防止在android one设备中进行屏幕截图。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Shailendra Madda    3 年前

    我们可以听 FileObserver 以检测何时捕捉到屏幕截图。然后从存储器中删除捕获的屏幕截图文件。

    以下是的示例代码 MediaListenerService

    import android.app.Service;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Environment;
    import android.os.FileObserver;
    import android.os.Handler;
    import android.os.IBinder;
    import android.os.Looper;
    import android.util.Log;
    import android.widget.Toast;
    
    import androidx.documentfile.provider.DocumentFile;
    
    import java.io.File;
    
    public class MediaListenerService extends Service {
    
        public static FileObserver observer;
        private Context context;
    
        public MediaListenerService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            context = this;
            startWatching();
        }
    
        private void startWatching() {
            //The desired path to watch or monitor
            String pathToWatch = Environment.getExternalStorageDirectory()
                    + File.separator + Environment.DIRECTORY_PICTURES
                    + File.separator + "Screenshots" + File.separator;
            Toast.makeText(this, "Service is Started and trying to watch " + pathToWatch, Toast.LENGTH_LONG).show();
    
            observer = new FileObserver(pathToWatch, FileObserver.ALL_EVENTS) { // set up a file observer to watch this directory
                @Override
                public void onEvent(int event, final String file) {
                    if (event == FileObserver.CREATE || event == FileObserver.CLOSE_WRITE || event == FileObserver.MODIFY || event == FileObserver.MOVED_TO && !file.equals(".probe")) { // check that it's not equal to .probe because thats created every time camera is launched
                        String filePath = pathToWatch + file;
                        Log.d("MediaListenerService", "File created [" + filePath + "]");
                        new Handler(Looper.getMainLooper()).postDelayed(() -> {
    //                        Toast.makeText(getBaseContext(), file + " was saved!", Toast.LENGTH_LONG).show();
                            File screenShotFile = new File(pathToWatch, file);
                            if (screenShotFile.exists()) {
                                try {
                                    boolean isDeleted = screenShotFile.delete();
                                    if (isDeleted) {
                                        Toast.makeText(getBaseContext(), file + " was deleted!", Toast.LENGTH_LONG).show();
                                        Log.d("MediaListenerService", "File deleted [" + filePath + "]");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }, 200L);
                    }
                }
            };
            observer.startWatching();
        }
    }
    

    在中添加文件权限 Manifest

    <uses-permission
            android:name="android.permission.WRITE_EXTERNAL_STORAGE"
            tools:ignore="ScopedStorage" />
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:requestLegacyExternalStorage="true"
    

    在中注册您的服务 显示 在application标签下:

    <service
      android:name=".MediaListenerService"
      android:enabled="true"
      android:exported="false" />
    

    并检查文件存储权限,然后在下面的MainActivity中启动服务 onCreate

    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                startService(Intent(baseContext, MediaListenerService::class.java))
            } else {
                ActivityCompat.requestPermissions(
                    this,
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    123
                )
            }
        }
    }