我在android中开发了一个应用程序。该应用程序包括通过webservice进行的服务器和客户端交互。我检查了wifi连接状态,如果连接状态为false,我已经显示了注意框,如果连接状态为true,我将执行一些操作。但我遇到的问题是LG,索尼和其他智能手机上的注意框都显示在除SAMSUNG之外的所有型号,包括Galaxy S4。
任何人都可以知道这个......
我将发布下面的代码,检查wifi,4g和状态。
boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX).getState() == NetworkInfo.State.CONNECTED)
{
// some action
//we are connected to a network
connected = true;
}
if(connected == false){
// System.out.println("The Network error");
attentionBox("There is no network connection right now. Please try again later.","Internet Connection Error");
}
和注意框编码是:
public void attentionBox(final String msg, final String title)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ViewYourPlanList.this);
builder.setMessage(msg)
.setTitle(title)
.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
//finish();
System.exit(0);
}
});
AlertDialog alert = builder.create();
alert.show();
}
答案 0 :(得分:0)
尝试这样
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
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;
}
答案 1 :(得分:0)
试试这个,它为我工作
public void showAlertDialog(Context context, String title, String message) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
如果它对您有用。投票给我。