在Android应用中不应允许屏幕录像

时间:2020-05-16 07:25:06

标签: java android android-studio

我不想在我的android应用程序中不进行屏幕录制,所以我在代码中添加了以下代码段作为监听器,以屏蔽所有android API级别记录的音频

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

可以使用以下代码段阻止视频录制

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

正在为Android版本6、7、9、10禁用视频并阻止屏幕录制,但是在Android版本8中,没有录制屏幕,但是可以收听音频。 请为我建议Android版本8的解决方案。

0 个答案:

没有答案