警报对话框冻结我的应用程序
Alert dialog freeze my app
这里有检查互联网连接的代码,如果 isconnected 为假,则在警报对话框中显示一条消息。问题是如果 isconnected 是真的并且(我试图用 if(isconnected) 子句代替 if(!isconnected))每一个都有效。但是,如果我在 VM 每次冻结时执行 show() 时都关闭 phone 上的每个网络。为什么?感谢所有人:
final AlertDialog.Builder dialog= new AlertDialog.Builder(this);
((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask asyncTask= new AsyncTask() {
boolean isconnected=true;
@Override
protected Object doInBackground(Object[] params) {
ConnectivityManager conMgr = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
isconnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
Log.i("StateNet",isconnected+"");
return null;
}
@Override
protected void onPostExecute(Object o) {
if(!isconnected){
dialog.setMessage("Controlla la tua conessione a internet")
.setTitle("Ops problemino con internet")
.setPositiveButton("Ok", ok)
.show();
}
super.onPostExecute(o);
}
};
您不需要 AsyncTask 来检查互联网连接,所以请保持简单。
我认为您的 onClick 方法应该如下所示:
@Override
public void onClick(View v) {
if (isConnectionAvailable(context)) {
// connected
} else {
// not connected
}
};
public static boolean isConnectionAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
不要忘记清单中的 ACCESS_NETWORK_STATE 和 INTERNET 权限。
试试这个...
((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isDeviceOnline(context)){
//do something.
}else{
AlertDialog.Builder dialog= new AlertDialog.Builder(context);
dialog.setMessage("Not connected");
dialog.setTitle("Error!");
dialog.setPositiveButton(.....);
dialog.create();
dialog.show();
}
}
}
public boolean isDeviceOnline(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
是的,在清单文件中添加 ACCESS_NETWORK_STATE 和 INTERNET 权限
这里有检查互联网连接的代码,如果 isconnected 为假,则在警报对话框中显示一条消息。问题是如果 isconnected 是真的并且(我试图用 if(isconnected) 子句代替 if(!isconnected))每一个都有效。但是,如果我在 VM 每次冻结时执行 show() 时都关闭 phone 上的每个网络。为什么?感谢所有人:
final AlertDialog.Builder dialog= new AlertDialog.Builder(this);
((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask asyncTask= new AsyncTask() {
boolean isconnected=true;
@Override
protected Object doInBackground(Object[] params) {
ConnectivityManager conMgr = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
isconnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
Log.i("StateNet",isconnected+"");
return null;
}
@Override
protected void onPostExecute(Object o) {
if(!isconnected){
dialog.setMessage("Controlla la tua conessione a internet")
.setTitle("Ops problemino con internet")
.setPositiveButton("Ok", ok)
.show();
}
super.onPostExecute(o);
}
};
您不需要 AsyncTask 来检查互联网连接,所以请保持简单。
我认为您的 onClick 方法应该如下所示:
@Override
public void onClick(View v) {
if (isConnectionAvailable(context)) {
// connected
} else {
// not connected
}
};
public static boolean isConnectionAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
不要忘记清单中的 ACCESS_NETWORK_STATE 和 INTERNET 权限。
试试这个...
((Button)findViewById(R.id.listabutton)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isDeviceOnline(context)){
//do something.
}else{
AlertDialog.Builder dialog= new AlertDialog.Builder(context);
dialog.setMessage("Not connected");
dialog.setTitle("Error!");
dialog.setPositiveButton(.....);
dialog.create();
dialog.show();
}
}
}
public boolean isDeviceOnline(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();
}
是的,在清单文件中添加 ACCESS_NETWORK_STATE 和 INTERNET 权限