Android 有没有办法等待连接?回电话?听众?

Android 有没有办法等待连接?回电话?听众?,android,android-intent,callback,settings,wait,Android,Android Intent,Callback,Settings,Wait,我正在做一个具有登录活动的应用程序,当我进入这个活动时,如果没有连接到我的服务器,我会要求用户启用WIFI或移动数据。我在对话框中给出了3个选项,WIFI、移动数据和取消 public void checkConnectionDialog(Context context){ if( dialogConnection != null && dialogConnection.isShowing() ) return; dialogConnectio

我正在做一个具有登录活动的应用程序,当我进入这个活动时,如果没有连接到我的服务器,我会要求用户启用WIFI或移动数据。我在对话框中给出了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();
    }
我有意将用户发送到WIFI或移动数据设置。如果用户启用wifi或数据并返回(按后退按钮),系统需要几秒钟才能连接到internet

如果用户在启用internet后单击“返回”,当他返回应用程序时,它仍然没有连接

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

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


感谢您的帮助。

您可以收到具有以下清单意图的连接更改。将它们作为目的过滤器放在接收器内。请注意,这只是从零开始的,我现在无法测试它

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

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

您可以接收具有以下清单意图的连接更改。将它们作为目的过滤器放在接收器内。请注意,这只是从零开始的,我现在无法测试它

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

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

您可以接收具有以下清单意图的连接更改。将它们作为目的过滤器放在接收器内。请注意,这只是从零开始的,我现在无法测试它

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

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

您可以接收具有以下清单意图的连接更改。将它们作为目的过滤器放在接收器内。请注意,这只是从零开始的,我现在无法测试它

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

例如,现在您可以启动一项服务,以执行一些操作来通知用户该服务是否已连接。

您可以注册一个
广播接收器
,该接收器监听您活动中的网络更改。大概是这样的:

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
}
注册和注销接收器
onPause
onResume

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

您可以注册一个
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
}
注册和注销接收器
onPause
onResume

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

您可以注册一个
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
}
注册和注销接收器
onPause
onResume

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

您可以注册一个
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
}
注册和注销接收器
onPause
onResume

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

创建一个BroadcastReceiver,当您连接到Internet时将触发事件:)创建一个BroadcastReceiver,当您连接到Internet时将触发事件:)创建一个BroadcastReceiver,当您连接到Internet时将触发事件:)创建一个BroadcastReceiver,当您连接到Internet时将触发事件互联网:)