无法解雇ProgressDialog(使用BroadcastReceiver)

时间:2013-03-03 17:30:19

标签: android broadcastreceiver progressdialog dismiss

我的问题是: 我希望我的应用程序在没有互联网连接时阻止按钮或整个活动,并在互联网连接恢复时取消阻止它们。我正在使用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。 BroadCast活动:

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。当互联网连接消失时显示,但是当互联网连接恢复时它不想消失。怎么解决?感谢。

2 个答案:

答案 0 :(得分: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()
    }
}

答案 1 :(得分:0)

保持对对话框的全局引用,这样您就不会继续重新创建它。您还应该尽可能使用原始类型(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();        
        }
    }