简体   繁体   English

防止Android一台设备截屏

[英]Prevent Screen Capture in Android One Devices

I want to prevent screenshot capture in android phones.我想防止在 android 手机中捕获屏幕截图。 I added the line我添加了行

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

This is working fine in normal devices.这在普通设备中工作正常。 But when I try on android one devices, ( https://www.android.com/intl/en_in/one/ ), it is still capturing screenshots.但是当我尝试 android 一台设备时,( https://www.android.com/intl/en_in/one/ ),它仍然在捕获屏幕截图。

I tried other apps like AmazonPrime, Hotstar, and GooglePay on android one devices... In which though they are capturing the screenshots, the content is entirely black.我在 android 一台设备上尝试了 AmazonPrime、Hotstar 和 GooglePay 等其他应用程序......虽然他们正在捕获屏幕截图,但内容完全是黑色的。 How to achieve that or prevent a screenshot in android one devices.如何实现或防止在 android 一台设备中截屏。

We can listen to FileObserver to detect when screenshot captured.我们可以监听FileObserver来检测截屏的时间。 Then delete the captured screenshot file from the storage.然后从存储中删除捕获的屏幕截图文件。

Here is the sample code for the MediaListenerService这是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();
    }
}

Add file permission in ManifestManifest中添加文件权限

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

Register your service in Manifest under application tag:在应用程序标签下的Manifest中注册您的服务:

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

And check file storage permission and start service in MainActivity under onCreate并在onCreate下的MainActivity中检查文件存储权限并启动服务

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM