gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:45:11 25 4
gpt4 key购买 nike

我想防止在安卓手机中截屏。我添加了行requireActivity().window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)这在普通设备中运行良好。但是当我在 android one 设备上尝试时,( https://www.android.com/intl/en_in/one/ ),它仍在捕获屏幕截图。
我在 android one 设备上尝试了其他应用程序,如 AmazonPrime、Hotstar 和 GooglePay……虽然它们正在捕获屏幕截图,但内容完全是黑色的。如何在 android one 设备中实现或防止屏幕截图。

最佳答案

我们可以收听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"
Manifest 注册您的服务在应用程序标签下:
<service
android:name=".MediaListenerService"
android:enabled="true"
android:exported="false" />
并在 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
)
}
}
}

关于java - 防止 Android One 设备中的屏幕捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69476311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com