关于屏幕方向:
阅读此页面上的答案后,我意识到我的应用程序在运行 AsyncTask 时无法处理方向更改。
这是我对这个问题的解决方案:
@Override
protected void onPreExecute() {
Device.lockOrientation((Activity)context);
...
}
@Override
protected void onPostExecute(List<Hydrant> hydrants) {
Device.releaseOrientation((Activity)context);
...
}
设备实现:
public class Device {
public static void lockOrientation(Activity activity) {
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int tempOrientation = activity.getResources().getConfiguration().orientation;
int orientation = 0;
switch(tempOrientation)
{
case Configuration.ORIENTATION_LANDSCAPE:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Configuration.ORIENTATION_PORTRAIT:
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
else
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
activity.setRequestedOrientation(orientation);
}
public static void releaseOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
由于 AsyncTasks 只打算运行几秒钟,这对我来说是一个可以接受的解决方案,因为这很少会影响用户。尤其是像 ProgressDialog 这样的东西,可以很好地表明她必须等待多长时间。