我的问题是:我希望我的应用程序在没有互联网连接时阻止按钮或整个活动,并在互联网连接恢复时取消阻止它们。我正在使用 BroadcastReceiver 在主要活动中检查互联网,它运行良好:
public class MainActivity extends BroadCast {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.registerReceiver(this.mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
// code
}
为了阻止 MainActivity,我使用了 ProgressDialog,方法 setCancelable 设置为 false。广播活动:
public class BroadCast extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
public BroadcastReceiver mConnReceiver = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
@SuppressWarnings("deprecation")
NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
if(currentNetworkInfo.isConnected()) {
BlockActivity(true);
}
else {
BlockActivity(false);
}
}
};
public void BlockActivity(Boolean connected)
{
final ProgressDialog pausingDialog = ProgressDialog.show(this, "", "Application waiting for internet connection...");
if (!connected)
{
Toast.makeText(getApplicationContext(), "Disconnected", Toast.LENGTH_LONG).show();
pausingDialog.show();
pausingDialog.setCancelable(false);
}
else
{
Toast.makeText(getApplicationContext(), "Connected", Toast.LENGTH_LONG).show();
pausingDialog.setCancelable(true);
pausingDialog.dismiss();
}
}
}
Toast 信息效果很好,但问题在于 ProgressDialog。当互联网连接消失时它会显示,但当互联网连接恢复时它不想消失。如何解决?谢谢。