mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-27 10:30:34 +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.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.ConnectivityManager;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.HandlerThread;
|
import android.os.HandlerThread;
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
@ -136,9 +135,7 @@ class CIMConnectorManager{
|
|||||||
|
|
||||||
public void connect(final String host, final int port) {
|
public void connect(final String host, final int port) {
|
||||||
|
|
||||||
boolean isNetworkConnected = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetwork() != null;
|
if (!CIMPushManager.isNetworkConnected(context)) {
|
||||||
|
|
||||||
if (!isNetworkConnected) {
|
|
||||||
|
|
||||||
Intent intent = new Intent();
|
Intent intent = new Intent();
|
||||||
intent.setPackage(context.getPackageName());
|
intent.setPackage(context.getPackageName());
|
||||||
|
@ -30,7 +30,6 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,9 +60,7 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
|||||||
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED)
|
if (intent.getAction().equals(CIMConstant.IntentAction.ACTION_NETWORK_CHANGED)
|
||||||
||intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||||
|
|
||||||
ConnectivityManager connectivityManager = (ConnectivityManager) context
|
onDevicesNetworkChanged();
|
||||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
||||||
onDevicesNetworkChanged(connectivityManager.getActiveNetworkInfo());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -133,9 +130,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
private void onInnerConnectionClosed() {
|
private void onInnerConnectionClosed() {
|
||||||
CIMCacheManager.putBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE, false);
|
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);
|
CIMPushManager.connect(context, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,9 +139,8 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void onConnectionFailed(long reinterval) {
|
private void onConnectionFailed(long reinterval) {
|
||||||
boolean isNetworkConnected = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetwork() != null;
|
|
||||||
|
|
||||||
if (isNetworkConnected) {
|
if (CIMPushManager.isNetworkConnected(context)) {
|
||||||
onConnectionFailed();
|
onConnectionFailed();
|
||||||
|
|
||||||
CIMPushManager.connect(context, reinterval);
|
CIMPushManager.connect(context, reinterval);
|
||||||
@ -159,13 +154,13 @@ public abstract class CIMEventBroadcastReceiver extends BroadcastReceiver {
|
|||||||
onConnectionSuccessed(autoBind);
|
onConnectionSuccessed(autoBind);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onDevicesNetworkChanged(NetworkInfo info) {
|
private void onDevicesNetworkChanged() {
|
||||||
|
|
||||||
if (info != null) {
|
if (CIMPushManager.isNetworkConnected(context)) {
|
||||||
CIMPushManager.connect(context, 0);
|
CIMPushManager.connect(context, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
onNetworkChanged(info);
|
onNetworkChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onInnerMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent) {
|
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 abstract void onMessageReceived(com.farsunset.cim.sdk.android.model.Message message, Intent intent);
|
||||||
|
|
||||||
public void onNetworkChanged(NetworkInfo info) {
|
public void onNetworkChanged() {
|
||||||
CIMListenerManager.notifyOnNetworkChanged(info);
|
CIMListenerManager.notifyOnNetworkChanged(CIMPushManager.getNetworkInfo(context));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onConnectionSuccessed(boolean hasAutoBind) {
|
public void onConnectionSuccessed(boolean hasAutoBind) {
|
||||||
|
@ -27,6 +27,8 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageInfo;
|
import android.content.pm.PackageInfo;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
@ -242,6 +244,15 @@ public class CIMPushManager {
|
|||||||
return CIMCacheManager.getBoolean(context, CIMCacheManager.KEY_CIM_CONNECTION_STATE);
|
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) {
|
private static String getVersionName(Context context) {
|
||||||
String versionName = null;
|
String versionName = null;
|
||||||
try {
|
try {
|
||||||
@ -260,4 +271,6 @@ public class CIMPushManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user