3

我的问题是:我希望我的应用程序在没有互联网连接时阻止按钮或整个活动,并在互联网连接恢复时取消阻止它们。我正在使用 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。当互联网连接消失时它会显示,但当互联网连接恢复时它不想消失。如何解决?谢谢。

4

2 回答 2

1

保持对对话框的全局引用,这样您就不会继续重新创建它。您还应该尽可能使用原始类型(boolean而不是Boolean)并遵守 Java 命名约定(camelCase)。

ProgressDialog pausingDialog;
public void blockActivity(boolean connected)
    {
        if (pausingDialog == null){
            pausingDialog = new ProgressDialog (this);
            pausingDialog.setMessage ("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();        
        }
    }
于 2013-03-03T17:35:07.040 回答
1

Kotlin 中的简单代码

var pausingDialog:SweetAlertDialog?=null
fun blockActivity(connected: Boolean,context: Context) {
    if (pausingDialog == null) {
        pausingDialog =SweetAlertDialog(context, SweetAlertDialog.ERROR_TYPE)
        pausingDialog!!.titleText = "Application waiting for internet connection..."
        pausingDialog!!.setCancelable(false)
        pausingDialog!!.setConfirmClickListener{
            var MyReceiver: BroadcastReceiver?= null;
            MyReceiver = com.example.myrecharge.Helper.MyReceiver()
            context.registerReceiver(MyReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
        }
    }
    if (!connected) {
        Toast.makeText(
            context,
            "Disconnected",
            Toast.LENGTH_LONG
        ).show()
        pausingDialog!!.show()
        Log.d("@@", "onReceive: 1")


    } else {
        Toast.makeText(
            context,
            "Connected",
            Toast.LENGTH_LONG
        ).show()
        pausingDialog!!.dismiss()
    }
}
于 2020-10-14T09:53:23.803 回答