mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-24 16:51:45 +08:00
优化android sdk代码
This commit is contained in:
parent
9e9cc962d5
commit
02bb5eb7f7
@ -47,7 +47,6 @@ import com.farsunset.cim.sdk.android.model.SentBody;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Process;
|
||||
@ -136,9 +135,7 @@ class CIMConnectorManager{
|
||||
|
||||
public void connect(final String host, final int port) {
|
||||
|
||||
boolean isNetworkConnected = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetwork() != null;
|
||||
|
||||
if (!isNetworkConnected) {
|
||||
if (!CIMPushManager.isNetworkConnected(context)) {
|
||||
|
||||
Intent intent = new Intent();
|
||||
intent.setPackage(context.getPackageName());
|
||||
|
@ -30,7 +30,6 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
|
||||
/**
|
||||
@ -61,9 +60,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED)
|
||||
||intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
onDevicesNetworkChanged(connectivityManager.getActiveNetworkInfo());
|
||||
onDevicesNetworkChanged();
|
||||
}
|
||||
|
||||
/*
|
||||
@ -133,9 +130,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
private void onInnerConnectionClosed() {
|
||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
||||
boolean isNetworkConnected = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetwork() != null;
|
||||
|
||||
if (isNetworkConnected) {
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
@ -143,9 +139,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
}
|
||||
|
||||
private void onConnectionFailed(long reinterval) {
|
||||
boolean isNetworkConnected = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetwork() != null;
|
||||
|
||||
if (isNetworkConnected) {
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
onConnectionFailed();
|
||||
|
||||
CIMPushManager.connect(context, reinterval);
|
||||
@ -159,13 +154,13 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
onConnectionSuccessed(autoBind);
|
||||
}
|
||||
|
||||
private void onDevicesNetworkChanged(NetworkInfo info) {
|
||||
private void onDevicesNetworkChanged() {
|
||||
|
||||
if (info != null) {
|
||||
if (CIMPushManager.isNetworkConnected(context)) {
|
||||
CIMPushManager.connect(context, 0);
|
||||
}
|
||||
|
||||
onNetworkChanged(info);
|
||||
onNetworkChanged();
|
||||
}
|
||||
|
||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent) {
|
||||
@ -183,8 +178,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
||||
|
||||
public abstract void onMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent);
|
||||
|
||||
public void onNetworkChanged(NetworkInfo info) {
|
||||
CIMListenerManager.notifyOnNetworkChanged(info);
|
||||
public void onNetworkChanged() {
|
||||
CIMListenerManager.notifyOnNetworkChanged(CIMPushManager.getNetworkInfo(context));
|
||||
}
|
||||
|
||||
public void onConnectionSuccessed(boolean hasAutoBind) {
|
||||
|
@ -27,6 +27,8 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
|
||||
@ -241,7 +243,16 @@ public class CIMPushManager {
|
||||
public static boolean isConnected(Context context) {
|
||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
||||
}
|
||||
|
||||
public static boolean isNetworkConnected(Context context) {
|
||||
NetworkInfo networkInfo = getNetworkInfo(context);
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
|
||||
public static NetworkInfo getNetworkInfo(Context context) {
|
||||
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
|
||||
}
|
||||
|
||||
private static String getVersionName(Context context) {
|
||||
String versionName = null;
|
||||
try {
|
||||
@ -259,5 +270,7 @@ public class CIMPushManager {
|
||||
context.startService(intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user