Android 拍摄当前显示屏幕的屏幕截图,而不是当前活动
我正在尝试制作一个小的Android应用程序,当出现特定通知时,它会触发当前显示屏幕的截图捕获。例如,我在whatsapp上,会出现一个whatsapp通知->触发whatsapp捕获 嗯,我当前的代码实际上检测到通知并在收到通知时触发屏幕截图,但不是我想要的方式。我会得到我主要活动的截图,即使它没有显示在屏幕上。我只想截屏它在屏幕上出现的内容。看起来很容易,但我做不到 我离开了当前的NotificationReceiver类,该类失败是因为它捕获了主活动而不是屏幕:Android 拍摄当前显示屏幕的屏幕截图,而不是当前活动,android,screenshot,Android,Screenshot,我正在尝试制作一个小的Android应用程序,当出现特定通知时,它会触发当前显示屏幕的截图捕获。例如,我在whatsapp上,会出现一个whatsapp通知->触发whatsapp捕获 嗯,我当前的代码实际上检测到通知并在收到通知时触发屏幕截图,但不是我想要的方式。我会得到我主要活动的截图,即使它没有显示在屏幕上。我只想截屏它在屏幕上出现的内容。看起来很容易,但我做不到 我离开了当前的NotificationReceiver类,该类失败是因为它捕获了主活动而不是屏幕: class Notific
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
}
}
你知道我该怎么做吗?我真的被卡住了。非常感谢您的帮助 您需要在Android 5.0+上使用,并将minSdkVersion
设置为21。出于隐私和安全原因,应用程序在没有明确用户许可的情况下不能截取其他应用程序的屏幕,而这只有在安卓5.0中才能实现
演示按需截图