我一直在开发一个软电话应用程序,当它正在交换连接时,它在重新注册到SIP服务器时有问题。我有一个处理BroadcastReciever的CONNECTIVITY_CHANGE,但我对两者之间的逻辑有异议。
我的代码:
@Override
public void onReceive(Context context, Intent intent) {
debugIntent(intent);
SipManager sipManager = (SipManager) context.getApplicationContext();
ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if(activeNetwork != null){
boolean isActiveConnected = activeNetwork != null && activeNetwork.isConnected();
LogText.appendLog(TAG + " Type: " + activeNetwork.getTypeName() + " isActiveConnected: " + isActiveConnected);
if(isActiveConnected){
sipManager.reRegister;
}
else {
sipManager.lostConnectionUpdate();
}
}
else {
sipManager.lostConnectionUpdate();
}
}
private void debugIntent(Intent intent) {
LogText.appendLog(TAG + " action: " + intent.getAction());
LogText.appendLog(TAG + " component: " + intent.getComponent());
Bundle extras = intent.getExtras();
if (extras != null) {
for (String key: extras.keySet()) {
LogText.appendLog(TAG + " key [" + key + "]: " +extras.get(key));
}
}
else {
LogText.appendLog(TAG + "no extras");
}
}问题:
我应该使用什么NetworkInfo?
从上下文或意图中获取我的NetworkInfo更好吗?
我查看我的日志,发现当我的debugIntent方法说我断开连接时,我的isActiveConnected布尔值无论如何都会返回true。
任何帮助都会很好。
谢谢
发布于 2014-12-12 18:56:03
我使用这种静态方法来检查设备是否具有连接性。效果很好。
public static boolean hasConnectivity(final Context context, final Intent intent) {
final ConnectivityManager mConnMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = null;
try {
info = ConnectivityManagerCompat.getNetworkInfoFromBroadcast(mConnMan, intent);
} catch (final Exception e) {
info = mConnMan.getActiveNetworkInfo();
}
return info != null && info.isConnectedOrConnecting();
}如果需要,可以将info.isConnectedOrConnecting()更改为info.isConnected()。
在您的广播接收器中,您可以呼叫:
boolean connected = hasConnectivity(context, intent);发布于 2014-12-12 19:06:18
这就是我要做的,请记住,只要代码正常工作,使用什么方法并不重要。
我在这里展示的代码有注释来解释可以做什么。
public class ConnectionBroadcast extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("android.net.conn.CONNECTIVITY_CHANGE")) {
//An instance of connectivity manager
ConnectivityManager conn = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
//In order to access methods created in the MainActivity from this broadcast
MainActivity main = (MainActivity)context;
//Get wifi and mobile connections
NetworkInfo wifi = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = conn
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnected() || mobile.isConnected()) {
//When the device is connected, connect or re-connect to the SIP server
//Example:
main.connectToSip()
} else {
//The device is not connected, disconnected from the sip server in order
//to re-register when the device gets connected again
main.disconnectSip();
}
}
}
}如果你还有任何问题,你可以告诉我,因为我只是花了大约5个月的工作在一个类似的项目。
发布于 2020-01-27 07:28:02
我试着用我的代码来解决你的问题。任何人都可以签入由kotlin代码使用的Github Android网络连接。尽管如此,它是被废弃的,但它的作用与魔法一样。
https://stackoverflow.com/questions/27450099
复制相似问题