0

我有一个执行 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。我怎样才能解决这个问题?

4

4 回答 4

2

问题是您可能(需要更多代码才能看到..)在您的活动中将其作为随机方法。

您的活动在方向更改时重新开始。不仅如此,而且当用户现在关闭您的应用程序时,您无法确保您的上传继续进行。

相反,您想要启动一个不受 Activity 生命周期约束的服务。有足够的教程,现在你知道要找什么了。你也可以试试 RoboSpice 库,它正是这样做的。

附言。使用 AsyncTask 不会解决这个问题,AsyncTask 本身仍将绑定到您的 Activity 的生命周期。

于 2013-08-02T08:23:27.600 回答
1

试试这个 将此行添加到清单中的活动中

<activity
....
android:configChanges="keyboard|keyboardHidden|orientation">
</activity>

尝试使用Asyc进行数据下载

于 2013-08-02T08:31:23.013 回答
0

发生这种情况是因为您的线程试图为已破坏的活动或上下文工作。尝试插入你的线程

onDestroy()

方法并确保在执行此操作时不要调用您的处理程序。

屏幕方向更改会破坏您当前的活动并再次创建它。

我希望它有所帮助。

于 2013-08-02T08:54:32.590 回答
0

显然,您需要将网络访问与您的 UI 分离。根据这篇文章,您可以在保留的Fragment(调用setRetainInstance(true);)中移动 UI 细节,从该 Fragment 启动一个 AsyncTask,然后......就是这样!

但是,我觉得您的 ANR 实际上并不是由此引起的,因为您可能正在 UI 线程中执行其他操作 - 可能是一些网络问题。我不确定是否updateProgress(prog, "Uploading " + thisFile.getName());确实发生在 UI 线程上(应该如此)。从您在活动中所做的事情中发布更多代码可能会揭示这一点。

于 2013-08-02T08:42:25.593 回答