1

我有一个使用 IntentService 运行后台任务的应用程序,我从网站中提取数据,解析数据,并根据结果创建日历事件。一切似乎都在创造,除了我遇到了旋转问题。使用下面的代码,当我旋转屏幕时,ProgressDialog 框保持可见,但在更新过程时永远不会用新文本更新,并且一旦调用完成就永远不会消失。我使用 IntentService 而不是 ASyncTask,因为用户还可以安排 IntentService 在其他时间运行,而无需与应用程序交互。任何帮助表示赞赏,谢谢!

public void onCreate(Bundle savedInstanceState) {
    Object retained = getLastNonConfigurationInstance();
    if (retained instanceof CalendarHandler) {
        // CH is a class level variable defined at the top which references my IntentService, aptly named CalendarHandler
        ch = (CalendarHandler) retained;
        ch.setActivity(this);
    } else {
        ch = null;
    }
    activity = this;

    btnLogin.setOnClickListener(OnClickListener(View view) {
            ch = new CalendarHandler();
            ch.setActivity(MyTlc.this);
            // Do other stuff, like run the intent service
    }
}

public Handler handler = new Handler() {
    public void handleMessage(Message message) {
        // We read the information from the message and do something with it
        // based on what the result code is
        String result = message.getData().getString("status");
        if (result.equals("ERROR")) {
            activity.removeDialog(PROGRESS_DIALOG);
            results.setText(message.getData().getString("error"));
        } else if (result.equals("DONE")) {
            activity.removeDialog(PROGRESS_DIALOG);
            int count = message.getData().getInt("count", 0);
            activity.results.setText("Added " + count + " shifts to the calendar");
        } else {
            activity.pDialog.setMessage(result);
        }
        super.handleMessage(message);
    }
};

据我了解,这应该可以工作,就像我说的 ProgressDialog 框确实保持正确,我似乎无法在旋转后将信息传递给对话框。

4

0 回答 0