public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//connMgr = (ConnectivityManager) this.getSystemService(CONNECTIVITY_SERVICE);
//if ((connMgr.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED)
// || (connMgr.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED)) {
if(new CheckInternetAvaliable(MobileReagentsActivity.this).isInternetAvaliable) {
morePrefs = this.getSharedPreferences("morePrefs", MODE_WORLD_READABLE);
final int welcomeScreenDisplay = 5000;
db = new VendorRatingDatabaseHandler(this);
isDataInserted = morePrefs.getBoolean("flagValue", false);
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
Intent intent = new Intent(MobileActivity.this, HomeScreen.class);
intent.putExtra("message", message);
intent.putExtra("topMolNames", topMolNames);
intent.putExtra("topFomls", topFomls);
intent.putExtra("lastConnect", lastConnect);
intent.putExtra("dateDiff", dateDiff);
intent.putExtra("moldrawPrefs", moldrawPrefs);
startActivity(intent);
Log.i("Test", "Starting Home Screen!! ");
MobileActivity.this.finish();
}
}
};
welcomeThread.start();
try {
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxP = saxPF.newSAXParser();
XMLReader xmlR = saxP.getXMLReader();
URL url = new URL(ConstantsClass.MORE_STARTUP_MESSAGE_URL + "?uid="+new AndroidUdIdClass(MobileActivity.this).androidId+"&device=android"); // URL of the XML
XMLHandler myXMLHandler = new XMLHandler();
xmlR.setContentHandler(myXMLHandler);
xmlR.parse(new InputSource(url.openStream()));
} catch (Exception e) {
System.out.println(e);
}
getsetXML = XMLHandler.data;
message = getsetXML.getMessage();
vendorListFull = getsetXML.getVendorListFull();
vendorListUser = getsetXML.getVendorListUser();
topMolNames = getsetXML.getTopMolNames();
topFomls = getsetXML.getTopFomls();
lastConnect = getsetXML.getLastConnect();
dateDiff = getsetXML.getDateDiff();
moldrawPrefs = getsetXML.getMoldrawPrefs();
prefsEditor = morePrefs.edit();
prefsEditor.putString("UserVendorList", vendorListUser);
prefsEditor.putString("VendorListFull", vendorListFull);
prefsEditor.commit();
//---------------------------------------------------------
String[] str_arr_vendor_list = vendorListFull.split(",");
for(int i=0; i<str_arr_vendor_list.length; i++) {
VendorRatingData vendorRatingData = new VendorRatingData();
vendorRatingData.setVendorName(str_arr_vendor_list[i]);
vendorRatingData.setVendorRating("0");
VendorRatingData vendor_rating = new VendorRatingData();
if(!isDataInserted) {
db.addVendorRating(vendorRatingData);
}else {
vendor_rating = db.getRating(str_arr_vendor_list[i]);
// Toast.makeText(getApplicationContext(), "vendor_rating::" + i, Toast.LENGTH_LONG).show();
if(!(vendor_rating.getVendorName().equalsIgnoreCase(str_arr_vendor_list[i]))) {
db.addVendorRating(vendorRatingData);
}
}
}
db.close();
prefsEditor = morePrefs.edit();
prefsEditor.putBoolean("flagValue", true);
prefsEditor.commit();
}
当数据获取需要时间时执行此代码时出现 ANR 错误,因此我想将 AsyncTask 添加到其中,但我不知道该怎么做。请帮我!