有没有办法等待连接?回调?听众?
Is there a way to wait for get connected? A callback? A listener?
我正在做一个有登录名 Activity 的应用程序,当我进入这个 activity 如果没有连接到我的服务器,我会要求用户启用 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();
}
通过 Intents,我将用户发送到 WIFI 或 MOBILE DATA 设置。如果用户启用 wifi 或数据并返回(按返回按钮),系统需要几秒钟才能连接到互联网。
如果用户在启用互联网后点击返回,当他 returns 到应用程序时它仍然没有连接。
我想知道的是如何让应用程序等待连接继续。
应用程序(未连接)--> 意图(已启用 WIFI 或数据)--> 应用程序(等待连接以显示视图)
感谢您的帮助。
您可以接收具有以下清单意图的连接更改。将它们作为 intent-filter 放入接收器中。请注意,这只是从头开始,我现在无法对其进行测试!
<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.....
}
}
例如,现在您可以启动一项服务来做一些事情来通知您的用户它是否已连接。
您可以注册一个 BroadcastReceiver
来侦听 activity 中的网络变化。像这样:
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
}
注册和取消注册接收器 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();
}
}
我正在做一个有登录名 Activity 的应用程序,当我进入这个 activity 如果没有连接到我的服务器,我会要求用户启用 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();
}
通过 Intents,我将用户发送到 WIFI 或 MOBILE DATA 设置。如果用户启用 wifi 或数据并返回(按返回按钮),系统需要几秒钟才能连接到互联网。
如果用户在启用互联网后点击返回,当他 returns 到应用程序时它仍然没有连接。
我想知道的是如何让应用程序等待连接继续。
应用程序(未连接)--> 意图(已启用 WIFI 或数据)--> 应用程序(等待连接以显示视图)
感谢您的帮助。
您可以接收具有以下清单意图的连接更改。将它们作为 intent-filter 放入接收器中。请注意,这只是从头开始,我现在无法对其进行测试!
<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.....
}
}
例如,现在您可以启动一项服务来做一些事情来通知您的用户它是否已连接。
您可以注册一个 BroadcastReceiver
来侦听 activity 中的网络变化。像这样:
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
}
注册和取消注册接收器 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();
}
}