我有一个扩展 AsyncTask 的类:
package Logic;
public class ReadConfig extends AsyncTask<String, Integer, String> {
ProgressBar progressBar;
Context context;
URL url;
FileInputStream config;
static CreateApplicationFolder folders;
File file;
DocumentBuilderFactory dbf;
DocumentBuilder db;
Document doc;
SharedPreferences prefs;
File configFile;
public ReadConfig(Context context, ProgressBar progressBar)
throws ParserConfigurationException, SAXException, IOException {
this.context = context;
this.progressBar = progressBar;
folders = new CreateApplicationFolder(context);
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
configFile = new File(folders.getPathToNode() + "/config.xml");
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
protected void onPreExecute() {
progressBar.incrementProgressBy(1);
}
@Override
protected String doInBackground(String... params) {
android.os.Debug.waitForDebugger();
int i;
if (!configFile.exists()) {
sleep();
return "noConfig";
} else {
try {
doc = db.parse(configFile);
} catch (SAXException e2) {
Log.e("SAX: ", e2.getMessage());
} catch (IOException e2) {
Log.e("IO: ", e2.getMessage());
}
doc.getDocumentElement().normalize();
Log.i("ROOT NODE: ", doc.getDocumentElement().getNodeName());
NodeList listOfMail = doc.getElementsByTagName("mail");
int totalMail = listOfMail.getLength();
Log.i("LENGTH: ", Integer.toString(totalMail));
for (i = 0; i < totalMail; i++) {
Node firstMailSetting = listOfMail.item(i);
Element e = (Element) firstMailSetting;
if (i == 0) {
putSharedPrefs("host", getTagValue("host", e));
putSharedPrefs("mail", getTagValue("account", e));
putSharedPrefs("port", getTagValue("port", e));
putSharedPrefs("pw", getTagValue("password", e));
} else if (i == 1) {
putSharedPrefs("supportMail", getTagValue("account", e));
} else if (i == 2) {
putSharedPrefs("Tomail", getTagValue("account", e));
}
sleep();
}
}
return "";
}
protected void onPostExecute(String result) {
if (result.equals("noConfig")) {
Toast.makeText(context, "No Config file was found..",
Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(context, MAIN.class);
context.startActivity(mainIntent);
} else {
Intent mainIntent = new Intent(context, MAIN.class);
context.startActivity(mainIntent);
}
}
private String getTagValue(String sTag, Element eElement) {
try {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
.getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
} catch (Exception e) {
return "";
}
}
public void putSharedPrefs(String tag, String value) {
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putString(tag, value);
prefEditor.commit();
}
public void sleep() {
for (int j = 0; j < 1; j++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
“问题”出现在以下代码行中:
if (!configFile.exists()) {
sleep();
return "noConfig";
这将向方法返回“noConfig”,该onPostExecute
方法应显示Toast
:
protected void onPostExecute(String result) {
if (result.equals("noConfig")) {
Toast.makeText(context, "No Config file was found..",
Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(context, MAIN.class);
context.startActivity(mainIntent);
} else {
Intent mainIntent = new Intent(context, MAIN.class);
context.startActivity(mainIntent);
}
}
此代码适用于三星 S3,但是当我在 HTC 上测试它时,屏幕会变黑,并且Toast
一直保持显示而无需启动新的Activity
. 没有错误,没有例外。有任何想法吗?
编辑
正在从我的初始屏幕调用 AsyncTask:
try {
new ReadConfig(Splash.this, pd).execute("");
} catch (ParserConfigurationException e) {
Log.e("Parser: ", e.getMessage());
} catch (SAXException e) {
Log.e("Sax: ", e.getMessage());
} catch (IOException e) {
Log.e("IO: ", e.getMessage());
}
pd
对象ProgressDialog
来自 my ,下面Layout
是基本流程:
启动画面初始化。doInBackground
在方法中解析的 xml 文件中的一些设置。如果配置文件不存在,只需发送一条消息并启动 Main Activity。