gpt4 book ai didi

android - 截取当前显示屏幕的屏幕截图,而不是当前 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:30:28 30 4
gpt4 key购买 nike

我正在尝试制作一个小的 Android 应用程序,当特定通知出现时触发当前显示屏幕的屏幕截图。例如,我在 whatsapp 上,出现一个 Whatsapp 通知 -> 这会触发 whatsapp 捕获。

好吧,我当前的代码实际上检测到通知并在收到通知时触发屏幕截图,但不是我想要的方式。我得到了我的 MainActivity 的屏幕截图,即使它没有显示在屏幕上。我只想截取屏幕上显示的内容。这看起来很容易,但我做不到!

我离开了当前的 NotificationReceiver 类,它失败了,因为它捕获了 MainActivity 而不是屏幕:

class NotificationReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getStringExtra("notification_event") + "\n" + txtView.getText();
txtView.setText(temp);

if (intent.getStringExtra("notification_event").contains("bet")) {
Log.i("Dentro", "dentro");

Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

File imageFile = new File(mPath);

FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
Log.i("Catch","Error dentro del if(contains)");
e.printStackTrace();
}
}//fin if
}
}

关于我该如何继续的任何想法?我真的卡住了。非常感谢您的帮助!

最佳答案

您需要使用 the media projection APIs在 Android 5.0+ 上,并将您的 minSdkVersion 设置为 21。出于隐私和安全原因,应用不能在未经用户明确许可的情况下截取其他应用的屏幕截图,这只有在 Android 5.0 中才有可能。

This sample app演示按需截取屏幕截图。

关于android - 截取当前显示屏幕的屏幕截图,而不是当前 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40292738/

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