【问题标题】:Is there a way to wait for get connected? A callback? A listener?有没有办法等待连接?回调?听众?
【发布时间】:2015-11-15 03:21:47
【问题描述】:

我正在做一个有登录活动的应用程序,当我进入这个活动时,如果我的服务器没有连接,我会要求用户启用 WIFI 或 MOBILE DATA。我在一个对话框中给出了 3 个选项,WIFI、MOBILE DATA 和 CANCEL。

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 或数据并返回(按返回按​​钮),系统需要几秒钟才能连接到互联网。

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

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

应用程序(未连接)--> 意图(启用 WIFI 或数据)--> 应用程序(等待连接以显示视图)

感谢您的帮助。

【问题讨论】:

  • 创建一个广播接收器,当你连接到互联网时它会触发一个事件:)

标签: android android-intent callback settings wait


【解决方案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();
            }
        }
    }
};

已建立和不存在的连接只是 void 方法,可以执行您想要的操作,例如:

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();
    }
}

【讨论】:

    【解决方案2】:

    您可以通过以下清单意图接收连接更改。将它们作为意图过滤器放入接收器中。请注意,这只是从头开始,我现在无法测试!

    <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.....
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      相关资源
      最近更新 更多