我一直在开发一个软件电话应用程序,它在切换连接时重新注册到SIP服务器时遇到问题。我有一个处理CONNECTIVITY_CHANGE的BroadcastReciever,但我遇到了逻辑问题。
我的代码:
@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。
任何帮助都会很棒。
由于
答案 0 :(得分:0)
我使用此静态方法检查设备是否具有连接。它工作得很好。
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);
答案 1 :(得分:0)
这就是我要做的,请记住,只要您的代码正常运行,使用什么方法并不重要。
我在这里向您展示的代码有解释可以做什么的评论。
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个月的时间从事类似项目。
答案 2 :(得分:0)
我试图用我的代码解决您的问题。任何人都可以签入我由kotlin代码使用的Github Android Network Connection。即使已弃用,但仍可像魔术一样工作