android - How to check Internet connection and spawn a Dialog if not connected -
my app internet based , when device has not got internet crashes due trying download files no success. have realised while typing if data requires goes offline crash. have tried reading documentation , trying add app appears need different have been reading.
- add 'no internet connection' dialog preferably on activity , not via toast.
- add 'data unavailable' dialog fragment within toast
i use mainactivity load fragments doubt put kind of code in show dialog on fragment's used area. either way add mainactivity , internet loading fragment.
mainactivity.class:
public class mainactivity extends sherlockfragmentactivity { private viewpager mviewpager; private tabswipe mtabswipe; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); new firstlauncheula(this).show(); mviewpager = new viewpager(this); mviewpager.setid(r.id.pager); mviewpager.setoffscreenpagelimit(2); setcontentview(mviewpager); final actionbar bar = getsupportactionbar(); bar.setnavigationmode(actionbar.navigation_mode_tabs); mtabswipe = new tabswipe(this, mviewpager); mtabswipe.addtab(bar.newtab().settext("games"), a_internet_service.class, null); mtabswipe.addtab(bar.newtab().settext("movies"), test_fragment.class, null); mtabswipe.addtab(bar.newtab().settext("tech"), purchase_fragment.class, null); } }
a_internet_service.class:
public class a_fragment_service extends fragment { // static variables static final string url = "this.is.a.url"; // xml node keys static final string key_data = "data"; // parent node static final string key_id = "id"; static final string key_title = "title"; static final string key_date1 = "date1"; static final string key_date2 = "date2"; static final string key_date3 = "date3"; static final string key_date2vis = "date2vis"; static final string key_date3vis = "date3vis"; static final string key_platforms = "platforms"; static final string key_thumb_url = "thumb_url"; arraylist<hashmap<string, string>> songslist; view view; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); new thetask().execute(); /* // click event single list row list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub } }); */ } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view = inflater.inflate(r.layout.main, container, false); //list = (listview) view.findviewbyid(r.id.data_list); return view; } @override public void onactivitycreated (bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); } class thetask extends asynctask<void, void, void> { @override protected void onpreexecute() { // todo auto-generated method stub super.onpreexecute(); } @override protected void doinbackground(void... params) { xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml url songslist = new arraylist<hashmap<string, string>>(); document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_data); (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_id, parser.getvalue(e, key_id)); map.put(key_title, parser.getvalue(e, key_title)); map.put(key_date1, parser.getvalue(e, key_date1)); map.put(key_date2, parser.getvalue(e, key_date2)); map.put(key_date3, parser.getvalue(e, key_date3)); map.put(key_date2vis, parser.getvalue(e, key_date2vis)); map.put(key_date3vis, parser.getvalue(e, key_date3vis)); map.put(key_platforms, parser.getvalue(e, key_platforms)); map.put(key_thumb_url, parser.getvalue(e, key_thumb_url)); // adding hashlist arraylist songslist.add(map); } return null; } @override protected void onpostexecute(void result) { // getting adapter passing xml data arraylist lazyadapter adapter = new lazyadapter(getactivity(), datalist); view = getview(); listview mlist = (listview) view.findviewbyid(r.id.data_list); mlist.setadapter(adapter); super.onpostexecute(result); } } }
edited fragment section:
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view = inflater.inflate(r.layout.main, container, false); return view; if(!isconnected(getactivity())) builddialog(getactivity()).show(); else { new thetask().execute(); } } public boolean isconnected(context context) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnectedorconnecting()) { android.net.networkinfo wifi = cm.getnetworkinfo(connectivitymanager.type_wifi); android.net.networkinfo mobile = cm.getnetworkinfo(connectivitymanager.type_mobile); if((mobile != null && mobile.isconnectedorconnecting()) || (wifi != null && wifi.isconnectedorconnecting())) return true; else return false; } else return false; } public alertdialog.builder builddialog(context c) { alertdialog.builder builder = new alertdialog.builder(c); builder.settitle("no internet connection."); builder.setmessage("you have no internet connection"); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); return builder; } //all other code
you example check internet connection this:
it return true if device has internet connection or have internet connection.
public boolean isconnected(context context) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnectedorconnecting()) { android.net.networkinfo wifi = cm.getnetworkinfo(connectivitymanager.type_wifi); android.net.networkinfo mobile = cm.getnetworkinfo(connectivitymanager.type_mobile); if((mobile != null && mobile.isconnectedorconnecting()) || (wifi != null && wifi.isconnectedorconnecting())) return true; else return false; } else return false; }
and how u use in application: (inside fragments oncreateview example, if device not connected, show dialog, see code below of how spawn simple dialog)
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view = inflater.inflate(r.layout.yourlayout, container, false); // getactivity() hand on context method // if call inside activity, replace getactivity() "this" if(!isconnected(getactivity())) builddialog(getactivity()).show(); else { // have internet connection, save connect internet here new thetask().execute(); } // other stuff return view; } public boolean isconnected(context context) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo netinfo = cm.getactivenetworkinfo(); if (netinfo != null && netinfo.isconnectedorconnecting()) { android.net.networkinfo wifi = cm.getnetworkinfo(connectivitymanager.type_wifi); android.net.networkinfo mobile = cm.getnetworkinfo(connectivitymanager.type_mobile); if((mobile != null && mobile.isconnectedorconnecting()) || (wifi != null && wifi.isconnectedorconnecting())) return true; else return false; } else return false; } public alertdialog.builder builddialog(context c) { alertdialog.builder builder = new alertdialog.builder(c); builder.settitle("no internet connection."); builder.setmessage("you have no internet connection"); builder.setpositivebutton("ok", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { dialog.dismiss(); } }); return builder; }
this can done anywhere in code, example in oncreate() method of activity, or in oncreateview() method of fragment - wherever desire connect internet, check first - internet stuff.
don't forget permissions:
<uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" />
i hope helps ;)
Comments
Post a Comment