In a project, it is often necessary to determine whether there is a network connection. Recently learned how to determine whether the software network, if there is no networking, pop-up prompts information, connect the network.
Effect:
(1) In the context of networking:
(2) In the case of not networking:
(3) Click "Detection Settings":
SOURCE Download: Http://xiazai.jb51.net/201605/yuanma/MyApplication (jb51.net). rar
To determine whether our software is networked, look at the code:
/**
* Detect network Connection
* *
@param con
* @return
/Public boolean isnetworkavailable (context con) {
Connectivitymanager cm = (Connectivitymanager) con
. Getsystemservice (context.connectivity_service);
if (cm = = null) return
false;
Networkinfo NetInfo = Cm.getactivenetworkinfo ();
if (NetInfo = = null) {return
false;
}
if (netinfo.isconnected ()) {return
true;
}
return false;
}
If there is no networking, pop-up prompt box prompts to set up a network connection:
/** * Prompts to set the Network Connection dialog box * * @param context/public void Shownetdialog (final context) {Mmateriald Ialog = new Materialdialog (context). Setmessage ("The furthest distance in the world is no net"). Setpositivebutton ("Check Settings", New View.onclicklisten
ER () {@Override public void OnClick (View v) {Intent Intent = null;
try {@SuppressWarnings ("deprecation") String sdkversion = Android.os.Build.VERSION.SDK; if (integer.valueof (sdkversion) > Ten) {intent = new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTIN
GS);
else {intent = new intent ();
ComponentName comp = new ComponentName ("Com.android.settings", "com.android.settings.WirelessSettings");
Intent.setcomponent (comp);
Intent.setaction ("Android.intent.action.VIEW");
} context.startactivity (Intent);
catch (Exception e) {e.printstacktrace (); } Mmaterialdialog. dismiss ();
}). Setnegativebutton ("Cancel", new View.onclicklistener () {@Override public void OnClick (View v) {
Mmaterialdialog.dismiss ();
}
});
Mmaterialdialog.show ();
}
How to do judgment processing:
/**
* To determine whether the network/
private void Checknet () {
if (!mchecknetwork.isnetworkavailable (Getapplication ())) {
Mchecknetwork.shownetdialog (mainactivity.this);
} else {
Toast.maketext (mainactivity.this,
"There is network, haha", toast.length_short). Show ();
}
The specific code is as follows:
Mainactivity.java
Package com.bzu.gxs.chectnetwork;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast; public class Mainactivity extends activity implements view.onclicklistener{private Checknetwork mchecknetwork =new Chec
Knetwork ();
Private Button Btn_check;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
/** * Click event * @param view/@Override public void OnClick (view view) {switch (View.getid ()) {
Case R.id.btn_check:checknet ();
}/** * To determine if network/private void Checknet () {if (!mchecknetwork.isnetworkavailable (Getapplication ())) {
Mchecknetwork.shownetdialog (Mainactivity.this);
}else {toast.maketext (Mainactivity.this, "There is network, haha", toast.length_short). Show (); }/** * Initialization/private void init (){Btn_check = (Button) Findviewbyid (R.id.btn_check);
Btn_check.setonclicklistener (this);
}
}
Checknetwork.java
Package com.bzu.gxs.chectnetwork;
Import Android.content.ComponentName;
Import Android.content.Context;
Import android.content.Intent;
Import Android.media.browse.MediaBrowser;
Import Android.net.ConnectivityManager;
Import Android.net.NetworkInfo;
Import Android.os.Build;
Import Android.view.View;
Import Me.drakeet.materialdialog.MaterialDialog;
/** * Network detection * Created by GXS on 2016/5/13.
* * Public class Checknetwork {private Materialdialog mmaterialdialog; /** * Detection Network connection * * @param con * @return/public boolean isnetworkavailable (context con) {Connectivit
Ymanager cm = (Connectivitymanager) con. Getsystemservice (Context.connectivity_service);
if (cm = = null) return false;
Networkinfo NetInfo = Cm.getactivenetworkinfo ();
if (NetInfo = = null) {return false;
} if (netinfo.isconnected ()) {return true;
return false; /** * Prompts to set the Network Connection dialog box * * @param context/public void shownetdialOG (Final context) {Mmaterialdialog = new Materialdialog (context). Setmessage ("The furthest distance in the world is no net"). SetPos Itivebutton ("Check Settings", new View.onclicklistener () {@Override public void OnClick (View v) {Intent inte
NT = NULL;
try {@SuppressWarnings ("deprecation") String sdkversion = Android.os.Build.VERSION.SDK; if (integer.valueof (sdkversion) > Ten) {intent = new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTIN
GS);
else {intent = new intent (); ComponentName comp = new ComponentName ("Com.android.settings", "com.android.settings.Wireles
Ssettings ");
Intent.setcomponent (comp);
Intent.setaction ("Android.intent.action.VIEW");
} context.startactivity (Intent);
catch (Exception e) {e.printstacktrace ();
} Mmaterialdialog.dismiss (); }). Setnegativebutton ("Cancel", new View.onclicklistener () {@Override public void OnClick (View v) {Mmaterialdialog.dismiss
();
}
});
Mmaterialdialog.show ();
}
}
Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android: paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= " Com.bzu.gxs.chectnetwork.MainActivity ">
<button
android:id=" @+id/btn_check "
android: Layout_width= "Wrap_content"
android:layout_height= "wrap_content" android:text=
"test"/>
Relativelayout>
Note: You need to include in the manifest file Androidmanifest.xml
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android: Name= "Android.permission.INTERNET"/>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.