我在activity onCreate方法中创建了Handler。这个Hanlder负责在10秒后拍摄屏幕截图。在run方法里面我使用了while(flag == true)并屏蔽了capture util flag == false,但这困扰了我的活动。我无法工作。由于活动被卡住,它会一次又一次地拍摄相同图像的屏幕。 我如何使用我的屏幕和我正在做的处理程序在10秒后拍摄屏幕截图? while循环卡住了我的应用程序。
它拍照但我无法处理我的活动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flag = true;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
while (flag == true) {
String SCREENSHOTS_LOCATIONS = Environment
.getExternalStorageDirectory().toString() + "/re/";
// Get root view
View view = getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
// Get current theme to know which background to use
final Theme theme = getTheme();
final TypedArray ta = theme
.obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = getResources().getDrawable(res);
// Draw background
background.draw(canvas);
// Draw views
view.draw(canvas);
FileOutputStream fos = null;
try {
final File sddir = new File(SCREENSHOTS_LOCATIONS);
if (!sddir.exists()) {
sddir.mkdirs();
}
fos = new FileOutputStream(SCREENSHOTS_LOCATIONS + x
+ ".jpg");
x++;
if (fos != null) {
if (!bitmap.compress(Bitmap.CompressFormat.JPEG,
90, fos)) {
Log.d("ScreenShot", "Compress/Write failed");
}
fos.flush();
fos.close();
}
} catch (Exception e) {
}
}
}
}, 1000);
}
答案 0 :(得分:0)
也许尝试使用AsyncTask或其他线程。 postDelayed()
函数附加到主UI线程并锁定您的应用程序,直到run()
完成(这从未发生,因为flag永远不等于false,因此while循环变为无限?)。
以下是使用ScheduledThreadPoolExecutor的示例:
private ScheduledThreadPoolExecutor exec = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flag = true;
exec = new ScheduledThreadPoolExecutor(5);
long interval = (long) 10; // 10 seconds for you
exec.scheduleAtFixedRate(new savePicTask(), 0, interval, TimeUnit.SECONDS);
}
class savePicTask implements Runnable {
@Override
public void run() {
while (flag == true) {
String SCREENSHOTS_LOCATIONS = Environment
.getExternalStorageDirectory().toString() + "/re/";
// Get root view
View view = getWindow().getDecorView().getRootView();
// Create the bitmap to use to draw the screenshot
final Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
// Get current theme to know which background to use
final Theme theme = getTheme();
final TypedArray ta = theme
.obtainStyledAttributes(new int[] { android.R.attr.windowBackground });
final int res = ta.getResourceId(0, 0);
final Drawable background = getResources().getDrawable(res);
// Draw background
background.draw(canvas);
// Draw views
view.draw(canvas);
FileOutputStream fos = null;
try {
final File sddir = new File(SCREENSHOTS_LOCATIONS);
if (!sddir.exists()) {
sddir.mkdirs();
}
fos = new FileOutputStream(SCREENSHOTS_LOCATIONS + x
+ ".jpg");
x++;
if (fos != null) {
if (!bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) {
Log.d("ScreenShot", "Compress/Write failed");
}
fos.flush();
fos.close();
}
} catch (Exception e) {
}
}
}
}
编辑1: 除了我正在使用
之外,这段代码对我有用numberOfSeconds = 0.5;
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(5);
long interval = (long) (1000* numberOfSeconds); // numberOfSeconds = 10 for you
exec.scheduleAtFixedRate(new savePicTask(), 0, interval, TimeUnit.MILLISECONDS);
而不是拍摄截图我正在用相机拍照。所以我不确定为什么它不适合你。