我有一个执行 HTTP 帖子的方法...
当用户在此过程中旋转设备时,会导致 ANR。
这是我的代码...
public void uploadTransactions(View v)
{
final ProgressDialog prog;
try
{
// disable screen rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
// setup progress dialog
prog = new ProgressDialog(this);
prog.setTitle("Uploading Transactions");
prog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
prog.setIcon(R.drawable.appicon);
prog.setCanceledOnTouchOutside(false);
prog.setCancelable(false);
prog.show();
// setup thread for the sync
Thread syncThread = new Thread();
syncThread = new Thread()
{
public void run()
{
String result = "";
String currentFileContents = "";
GetByREST gbr = new GetByREST();
String URL = //url for ReST service
String URLparam = "";
String accountNo = //authorisation info
String emailAddress = //authorisation info
String password = //authorisation info
try
{
// loop through the files
File file = new File(SALES_DIRECTORY);
for (File thisFile : file.listFiles())
{
currentFileContents = readTransactions(thisFile);
updateProgress(prog, "Uploading " + thisFile.getName());
result = gbr.getRestOutput(URL, URLparam, accountNo, emailAddress, password, "3", new StringEntity(currentFileContents));
}
prog.dismiss();//dismiss the dialog
// ...re-enable rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
catch (Exception e)
{
messageBox("uploadTransactions: syncThread", e.getMessage());
}
}
};
syncThread.start();
}
catch (Exception e)
{
messageBox("uploadTransactions", e.getMessage());
}
}
如果屏幕是纵向的,这可以正常工作,但是在横向时,方向会返回纵向并导致 ANR。我怎样才能解决这个问题?