oratis
oratis

Reputation: 828

How to use a thread to parse xml file in a splash screen?

I made a splash screen and used a runnable thread to parse a xml file from server, but when I run this program , it just stuck at the splash screen, and I used a wifimanage to judge whether the device is connected to the Internet or not, if it is not, then skip the parsing and jump to the main activity.

here are some of my codes, would someone be so kind to check it out?

private static WifiManager mWifiManager;
    private static UpdateInfo info;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading);

        mWifiManager = (WifiManager)
                this.getSystemService(Context.WIFI_SERVICE);
    }
    public UpdateInfo getUpdateInfo(InputStream is) throws Exception{  
        if (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
        XmlPullParser  parser = Xml.newPullParser();    
        parser.setInput(is, "utf-8");//设置解析的数据源   
        int type = parser.getEventType();  
        info = new UpdateInfo();//实体  
        while(type != XmlPullParser.END_DOCUMENT ){  
            switch (type) {  
            case XmlPullParser.START_TAG:  
                if("version".equals(parser.getName())){  
                    info.setVersion(parser.nextText()); //获取版本号  
                }else if ("url".equals(parser.getName())){  
                    info.setUrl(parser.nextText()); //获取要升级的APK文件  
                }else if ("description".equals(parser.getName())){  
                    info.setDescription(parser.nextText()); //获取该文件的信息  
                }  
                break;  
            }  
            type = parser.next();  
        }  
        }else{
             Intent intent = new Intent ();
             intent.setClass(loading.this, monkeynote.class);
             startActivity(intent);
           }

        return info;  

    } 

Upvotes: 0

Views: 333

Answers (1)

Kurtis Nusbaum
Kurtis Nusbaum

Reputation: 30835

Your code only launches the activity if the loading fails. You need to move the intent creation outside of the else statement. Change your method to this:

public UpdateInfo getUpdateInfo(InputStream is) throws Exception{  
  if (mWifiManager.getWifiState()==WifiManager.WIFI_STATE_ENABLED){
    XmlPullParser  parser = Xml.newPullParser();    
    parser.setInput(is, "utf-8");//设置解析的数据源   
    int type = parser.getEventType();  
    info = new UpdateInfo();//实体  
    while(type != XmlPullParser.END_DOCUMENT ){  
        switch (type) {  
        case XmlPullParser.START_TAG:  
            if("version".equals(parser.getName())){  
                info.setVersion(parser.nextText()); //获取版本号  
            }else if ("url".equals(parser.getName())){  
                info.setUrl(parser.nextText()); //获取要升级的APK文件  
            }else if ("description".equals(parser.getName())){  
                info.setDescription(parser.nextText()); //获取该文件的信息  
            }  
            break;  
        }  
        type = parser.next();  
    }  
  }
  Intent intent = new Intent ();
  intent.setClass(loading.this, monkeynote.class);
  startActivity(intent);


  return info;  

} 

Upvotes: 1

Related Questions