<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
private boolean getNetWorkStatus() {
boolean netSataus = false;
ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cwjManager.getActiveNetworkInfo();
if (cwjManager.getActiveNetworkInfo() != null) {
netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
}
if (!netSataus) {
Builder b = new AlertDialog.Builder(this).setTitle("没有可用的网络")
.setMessage("是否对网络进行设置?");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent mIntent = new Intent("/");
ComponentName comp = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
startActivityForResult(mIntent,0);
} }).setNeutralButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
} }).show(); }
return netSataus;
}
本文介绍了一种在Android应用中检查网络连接状态的方法,并在无网络时提示用户进行网络设置。通过使用ConnectivityManager获取网络信息,判断网络是否可用,并在不可用时弹出对话框询问用户是否进入设置页面。
956

被折叠的 条评论
为什么被折叠?



