我们可以听
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"
在中注册您的服务
显示
在application标签下:
<service
android:name=".MediaListenerService"
android:enabled="true"
android:exported="false" />
并检查文件存储权限,然后在下面的MainActivity中启动服务
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
)
}
}
}