欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android网络编程之android连接网络的简单示例代码

程序员文章站 2022-06-04 12:17:13
复制代码 代码如下:private void calltowebsrvice() {    connectivitymanager connm...

复制代码 代码如下:

private void calltowebsrvice() {

    connectivitymanager connmanager = (connectivitymanager)getsystemservice(context.connectivity_service);

    if (connmanager.getnetworkinfo(connectivitymanager.type_mobile).getstate() == networkinfo.state.connected ||  connmanager .getnetworkinfo(connectivitymanager.type_wifi).getstate() == networkinfo.state.connected  ) {
       _progresssdialog = progressdialog.show(this,"please wait...","retrieving data");
       getdata(); 
    } else
        toast.maketext(bestscore.this, "network error!!!", toast.length_long).show();          

}
public void getdata(){

    thread _thread = new thread(){

        public void run(){

            message _msg = new message();
            _msg.what = _updatasmslog;
            calldatabase();
            bestscore.this._uicallback.sendmessage(_msg);
        }
    };
    _thread.start();
}

handler _uicallback = new handler(){

    public void handlemessage(message msg) {

        switch(msg.what){

            case _updatasmslog :

                if(_datalist.size() > 0 ){

                        _initial.settext(((data)(_datalist.get(0)))._initial);
                        _wpm.settext(((data)(_datalist.get(0)))._wpm);
                        _phone.settext(((data)(_datalist.get(0)))._smartphone);
                        _country.settext(((data)(_datalist.get(0)))._country);

                    _progresssdialog.dismiss();
                    setlistadapter(new iconicadator(bestscore.this,r.layout.viewrow,_datalist));
                }
                else{

                    _progresssdialog.dismiss();
                    toast.maketext(bestscore.this,"no record found",toast.length_long).show();
                }
        }

    };

};