如果网络不可用,我的代码不会显示 toast 消息。这是我下面的代码...如果没有可用的网络吐司将不会显示。我的代码中有什么错误?请帮帮我。
public class HomeMenu extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homemenu);
if (!Utils.isNetworkAvailable(HomeMenu.this)) {
Toast.makeText(HomeMenu.this, getString(R.string.no_internet), Toast.LENGTH_SHORT).show();
}
}
}
实用类:
public static boolean isNetworkAvailable(Activity activity) {
ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}