简体   繁体   中英

Prevent Screen Capture in Android One Devices

I want to prevent screenshot capture in android phones. 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.

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. How to achieve that or prevent a screenshot in android one devices.

We can listen to FileObserver to detect when screenshot captured. Then delete the captured screenshot file from the storage.

Here is the sample code for the 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 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"

Register your service in Manifest under application tag:

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

And check file storage permission and start service in MainActivity under 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
            )
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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