Screen recording should not be allowed in the Android app

Viewed 157

I don't want screen recording to be not done in my android app, so I have added the below snippet to my code as a listener for muting the audio that gets recorded in all android API level

  public class AudioListners {
​
    private Context context;
​
    public AudioListners(Context context) {
        this.context = context;
    }
​
    private boolean isAvalible = true;
    private ChangeListener listener;
    private Handler handler;
    private Runnable runnable;
    private MediaRecorder[] recorder;
​
    private void setBoo(boolean isAvalible) {
        if (listener != null) listener.onChange(isAvalible);
    }
​
    public ChangeListener getListener() {
        return listener;
    }
​
    public void setListener(ChangeListener listener) {
        this.listener = listener;
​
​
​
        //for testing device api-24(N)
//        getMicrophoneAvailable();
​
        //for api-29(Q)
        getMicrophoneAvailable2();
​
​
//        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {
//
//        }else {
//
//        }
    }
​
    public interface ChangeListener {
        void onChange(boolean isAvalible);
    }
​
    private void getMicrophoneAvailable() {
        final MediaRecorder[] recorder = new MediaRecorder[1];
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    recorder[0] = new MediaRecorder();
                    recorder[0].setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                    recorder[0].setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
                    recorder[0].setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    recorder[0].setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
                    recorder[0].prepare();
                    recorder[0].start();
                    setBoo(true);
                    Log.d("audio_txt1", "getMicrophoneAvailable:success "+true);
​
                }
                catch (Exception exception) {
                    setBoo(false);
                    Log.d("audio_txt1", "getMicrophoneAvailable:exception "+exception.getMessage());
                }
                recorder[0].release();
​
                handler.postDelayed(runnable, 2000);
            }
        };
        handler.postDelayed(runnable, 2000);
    }
​
    private void getMicrophoneAvailable2() {
        stopListner();
        recorder = new MediaRecorder[1];
        try {
            recorder[0] = new MediaRecorder();
            recorder[0].setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
            recorder[0].setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
            recorder[0].setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            recorder[0].setOutputFile(new File(context.getCacheDir(), "MediaUtil#micAvailTestFile").getAbsolutePath());
            recorder[0].prepare();
            recorder[0].start();
            setBoo(true);
            Log.d("audio_txt1", "getMicrophoneAvailable:success "+true);
​
        }
        catch (Exception exception) {
            setBoo(false);
            Log.d("audio_txt1", "getMicrophoneAvailable:exception "+exception.getMessage());
        }
    }
​
    public void stopListner() {
        //for testing device api-24(N)
//        try {
//            handler.removeCallbacks(runnable);
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
​
        //for api-29(Q)
        try {
            recorder[0].release();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Video Recording can be blocked using the below snippet

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

Video is getting muted and preventing screen recording for the Android version 6, 7, 9, 10 but in Android version 8 the screen is not getting recorded but audio I am able to listen. Please suggest me solution for the android version 8.

0 Answers
Related