有没有办法等待连接?回调?听众?

时间:2015-08-21 08:39:50

标签: android android-intent callback settings wait

我正在做一个有登录活动的应用程序,当我进入此活动时,如果没有连接到我的服务器,我要求用户启用WIFI或MOBILE DATA。我在对话框中提供3个选项,WIFI,移动数据和取消。

public void checkConnectionDialog(Context context){

        if( dialogConnection != null && dialogConnection.isShowing() ) return;

        dialogConnection = new Dialog(context);
        dialogConnection.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogConnection.setCanceledOnTouchOutside(false);
        dialogConnection.setCancelable(false);

        dialogConnection.setContentView(R.layout.dialog_connection);

        Button bt_data = (Button) dialogConnection.findViewById(R.id.bt_data);
        Button bt_wifi = (Button) dialogConnection.findViewById(R.id.bt_wifi);
        Button bt_cancel = (Button) dialogConnection.findViewById(R.id.bt_cancel);


        bt_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
                dialogConnection.dismiss();
            }
        });

        bt_wifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
                dialogConnection.dismiss();
            }
        });

        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogConnection.dismiss();
                setResult(RESULT_CANCELED, null);
                finish();
            }
        });

        dialogConnection.show();
    }

使用Intents,我将用户发送到WIFI或MOBILE DATA设置。如果用户启用了wifi或数据并返回(按后退按钮),系统需要几秒钟才能连接到互联网。

如果用户在启用互联网后立即点击,当他返回应用程序时,它仍然没有连接。

我想知道的是如何让应用程序等待连接继续。

APP(未连接) - >意图(启用WIFI或数据) - > APP(等待直接连接显示视图)

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以注册一个BroadcastReceiver来监听您的活动中的网络更改。像这样:

BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction().equals(ConnectivityManager
                .CONNECTIVITY_ACTION)) {

            boolean isConnected = ConnectivityManagerCompat.getNetworkInfoFromBroadcast
                    ((ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE),
                            intent).isConnected();

            if (isConnected) {
                onConnectionEstablished();
            } else {
                onConnectionAbsent();
            }
        }
    }
};

建立和缺席的连接只是无效的方法,可以做你想要的事情,比如:

public void onConnectionEstablished() {
       // do something
}

public void onConnectionAbsent() {
       // do something
}

注册并取消注册接收者onPauseonResume

@Override
protected void onResume() {
    super.onResume();
    IntentFilter filter = new IntentFilter();
    filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
    registerReceiver(connectionReceiver, filter);
}

@Override
protected void onPause() {
    super.onPause();
    try {
        unregisterReceiver(connectionReceiver);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

答案 1 :(得分:0)

您可以使用以下清单意图接收连接更改。将它们作为intent-filter放在reciver中。请注意,这只是从头开始,我暂时无法测试它!

<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        <action android:name="android.net.wifi.STATE_CHANGE"/>

每次连接更改时都会收到此消息,但您必须在BroadcastReceiver中进行一些工作以检查它是否已连接:

例如,您可以收听onReceive():

@Override
public void onReceive(Context context, final Intent intent) {
    ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    if (info.isConnected()) {
        //then You know it is connected and can do some stuff
    }else{

      //not connected.....
    }
}

例如,现在您可以启动服务来做一些事情来告知您的用户它是否已连接。

相关问题