gpt4 book ai didi

android - 在 android 中收听屏幕截图操作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:37:43 27 4
gpt4 key购买 nike

我是 android 开发的新手。

我正在制作一个应用程序,它会在截取 Android 手机的屏幕截图时使用react。我听说 android 允许这样的操作可以被 Broadcast Receivers 检测到,所以我浏览了 android 开发人员文档,here .

我认为框架开发人员忘记或没有实现屏幕截图广播代码,因为他们没有在文档中列出此操作。

有没有其他方法可以让我听到屏幕截图采取行动?

最佳答案

您可以使用 ContentObserver 检测截屏事件。我正在我的一个项目中使用它。

ScreenShotContentObserver.java

public abstract class ScreenShotContentObserver extends ContentObserver {

private Context context;
private boolean isFromEdit = false;
private String previousPath;

public ScreenShotContentObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}

@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}

@Override
public void onChange(boolean selfChange, Uri uri) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, new String[]{
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA
}, null, null, null);
if (cursor != null && cursor.moveToLast()) {
int displayNameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
String fileName = cursor.getString(displayNameColumnIndex);
String path = cursor.getString(dataColumnIndex);
if (new File(path).lastModified() >= System.currentTimeMillis() - 10000) {
if (isScreenshot(path) && !isFromEdit && !(previousPath != null && previousPath.equals(path))) {
onScreenShot(path, fileName);
}
previousPath = path;
isFromEdit = false;
} else {
cursor.close();
return;
}
}
} catch (Throwable t) {
isFromEdit = true;
} finally {
if (cursor != null) {
cursor.close();
}
}
super.onChange(selfChange, uri);
}

private boolean isScreenshot(String path) {
return path != null && path.toLowerCase().contains("screenshot");
}

protected abstract void onScreenShot(String path, String fileName);

}

并在您的 Activity 中使用此类:-

public class MainActivity extends AppCompatActivity {

private ScreenShotContentObserver screenShotContentObserver;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

HandlerThread handlerThread = new HandlerThread("content_observer");
handlerThread.start();
final Handler handler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};

screenShotContentObserver = new ScreenShotContentObserver(handler, this) {
@Override
protected void onScreenShot(String path, String fileName) {
File file = new File(path); //this is the file of screenshot image
}
};

}

@Override
public void onResume() {
super.onResume();

getContentResolver().registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,
screenShotContentObserver
);
}

@Override
public void onPause() {
super.onPause();

try {
getContentResolver().unregisterContentObserver(screenShotContentObserver);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void onDestroy() {
super.onDestroy();
try {
getContentResolver().unregisterContentObserver(screenShotContentObserver);
} catch (Exception e) {
e.printStackTrace();
}
}

}

不要忘记在 ActivityonPauseonDestroy 方法中停止 Observer

关于android - 在 android 中收听屏幕截图操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31360296/

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