mirror of
https://gitee.com/farsunset/cim.git
synced 2025-07-17 13:36:16 +08:00
Update CIMEnventListenerReceiver.java
This commit is contained in:
parent
95b5f9d74c
commit
1aeb11b361
@ -1,176 +1,188 @@
|
|||||||
package com.farsunset.cim.client.android;
|
package com.farsunset.cim.client.android;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.ActivityManager.RunningTaskInfo;
|
import android.app.ActivityManager.RunningTaskInfo;
|
||||||
import android.content.BroadcastReceiver;
|
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.net.NetworkInfo;
|
||||||
|
import android.os.Handler;
|
||||||
import com.farsunset.cim.nio.constant.CIMConstant;
|
import com.farsunset.cim.nio.constant.CIMConstant;
|
||||||
import com.farsunset.cim.nio.mutual.Message;
|
import com.farsunset.cim.nio.mutual.Message;
|
||||||
import com.farsunset.cim.nio.mutual.ReplyBody;
|
import com.farsunset.cim.nio.mutual.ReplyBody;
|
||||||
import com.farsunset.cim.nio.mutual.SentBody;
|
import com.farsunset.cim.nio.mutual.SentBody;
|
||||||
/**
|
/**
|
||||||
* 消息入口,所有消息都会经过这里
|
* 消息入口,所有消息都会经过这里
|
||||||
* @author 3979434
|
* @author 3979434
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public abstract class CIMEnventListenerReceiver extends BroadcastReceiver implements OnCIMMessageListener {
|
public abstract class CIMEnventListenerReceiver extends BroadcastReceiver implements OnCIMMessageListener {
|
||||||
|
|
||||||
|
|
||||||
public Context context;
|
public Context context;
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context ctx, Intent it) {
|
public void onReceive(Context ctx, Intent it) {
|
||||||
|
|
||||||
context = ctx;
|
context = ctx;
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_NETWORK_CHANGED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_NETWORK_CHANGED))
|
||||||
{
|
{
|
||||||
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
|
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
|
||||||
android.net.NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
android.net.NetworkInfo info = connectivityManager.getActiveNetworkInfo();
|
||||||
|
|
||||||
onDevicesNetworkChanged(info);
|
onDevicesNetworkChanged(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_CLOSED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_CLOSED))
|
||||||
{
|
{
|
||||||
dispatchConnectionClosed();
|
dispatchConnectionClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_FAILED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_FAILED))
|
||||||
{
|
{
|
||||||
onConnectionFailed((Exception) it.getSerializableExtra("exception"));
|
onConnectionFailed((Exception) it.getSerializableExtra("exception"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_SUCCESS))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_SUCCESS))
|
||||||
{
|
{
|
||||||
dispatchConnectionSucceed();
|
dispatchConnectionSucceed();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_MESSAGE_RECEIVED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_MESSAGE_RECEIVED))
|
||||||
{
|
{
|
||||||
filterType999Message((Message)it.getSerializableExtra("message"));
|
filterType999Message((Message)it.getSerializableExtra("message"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_REPLY_RECEIVED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_REPLY_RECEIVED))
|
||||||
{
|
{
|
||||||
onReplyReceived((ReplyBody)it.getSerializableExtra("replyBody"));
|
onReplyReceived((ReplyBody)it.getSerializableExtra("replyBody"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_FAILED))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_FAILED))
|
||||||
{
|
{
|
||||||
onSentFailed((Exception) it.getSerializableExtra("exception"),(SentBody)it.getSerializableExtra("sentBody"));
|
onSentFailed((Exception) it.getSerializableExtra("exception"),(SentBody)it.getSerializableExtra("sentBody"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_SUCCESS))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_SENT_SUCCESS))
|
||||||
{
|
{
|
||||||
onSentSucceed((SentBody)it.getSerializableExtra("sentBody"));
|
onSentSucceed((SentBody)it.getSerializableExtra("sentBody"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_UNCAUGHT_EXCEPTION))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_UNCAUGHT_EXCEPTION))
|
||||||
{
|
{
|
||||||
onUncaughtException((Exception)it.getSerializableExtra("exception"));
|
onUncaughtException((Exception)it.getSerializableExtra("exception"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_STATUS))
|
if(it.getAction().equals(CIMConnectorManager.ACTION_CONNECTION_STATUS))
|
||||||
{
|
{
|
||||||
onConnectionStatus(it.getBooleanExtra(CIMPushManager.KEY_CIM_CONNECTION_STATUS, false));
|
onConnectionStatus(it.getBooleanExtra(CIMPushManager.KEY_CIM_CONNECTION_STATUS, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void dispatchConnectionClosed() {
|
private void dispatchConnectionClosed() {
|
||||||
|
|
||||||
if(CIMConnectorManager.netWorkAvailable(context))
|
if(CIMConnectorManager.netWorkAvailable(context))
|
||||||
{
|
{
|
||||||
CIMPushManager.init(context);
|
CIMPushManager.init(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
onConnectionClosed();
|
onConnectionClosed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean isInBackground(Context context) {
|
protected boolean isInBackground(Context context) {
|
||||||
List<RunningTaskInfo> tasksInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
|
List<RunningTaskInfo> tasksInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
|
||||||
if (tasksInfo.size() > 0) {
|
if (tasksInfo.size() > 0) {
|
||||||
|
|
||||||
if (context.getPackageName().equals(
|
if (context.getPackageName().equals(
|
||||||
tasksInfo.get(0).topActivity.getPackageName())) {
|
tasksInfo.get(0).topActivity.getPackageName())) {
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onConnectionFailed(Exception e){
|
private void onConnectionFailed(Exception e){
|
||||||
|
|
||||||
if(CIMConnectorManager.netWorkAvailable(context))
|
if(CIMConnectorManager.netWorkAvailable(context))
|
||||||
{
|
{
|
||||||
CIMPushManager.init(context);
|
//间隔30秒后重连
|
||||||
}
|
connectionHandler.sendMessageDelayed(connectionHandler.obtainMessage(), 30*1000);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private void dispatchConnectionSucceed() {
|
|
||||||
|
|
||||||
CIMPushManager.setAccount(context);
|
Handler connectionHandler = new Handler()
|
||||||
onConnectionSucceed();
|
{
|
||||||
|
@Override
|
||||||
}
|
public void handleMessage(android.os.Message message){
|
||||||
|
|
||||||
|
CIMPushManager.init(context);
|
||||||
|
}
|
||||||
private void onUncaughtException(Throwable arg0) {}
|
|
||||||
|
};
|
||||||
|
|
||||||
|
private void dispatchConnectionSucceed() {
|
||||||
private void onDevicesNetworkChanged(NetworkInfo info) {
|
|
||||||
|
CIMPushManager.setAccount(context);
|
||||||
if(info !=null)
|
onConnectionSucceed();
|
||||||
{
|
|
||||||
CIMPushManager.init(context);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
onNetworkChanged(info);
|
|
||||||
}
|
private void onUncaughtException(Throwable arg0) {}
|
||||||
|
|
||||||
private void filterType999Message(com.farsunset.cim.nio.mutual.Message message)
|
|
||||||
{
|
|
||||||
if(CIMConstant.MessageType.TYPE_999.equals(message.getType()))
|
private void onDevicesNetworkChanged(NetworkInfo info) {
|
||||||
{
|
|
||||||
CIMDataConfig.putBoolean(context,CIMDataConfig.KEY_MANUAL_STOP,true);
|
if(info !=null)
|
||||||
}
|
{
|
||||||
|
CIMPushManager.init(context);
|
||||||
onMessageReceived(message);
|
}
|
||||||
}
|
|
||||||
|
onNetworkChanged(info);
|
||||||
private void onSentFailed(Exception e, SentBody body){
|
}
|
||||||
|
|
||||||
//与服务端端开链接,重新连接
|
private void filterType999Message(com.farsunset.cim.nio.mutual.Message message)
|
||||||
if(e instanceof CIMSessionDisableException)
|
{
|
||||||
{
|
if(CIMConstant.MessageType.TYPE_999.equals(message.getType()))
|
||||||
CIMPushManager.init(context);
|
{
|
||||||
}else
|
CIMDataConfig.putBoolean(context,CIMDataConfig.KEY_MANUAL_STOP,true);
|
||||||
{
|
}
|
||||||
//发送失败 重新发送
|
|
||||||
CIMPushManager.sendRequest(context, body);
|
onMessageReceived(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
private void onSentFailed(Exception e, SentBody body){
|
||||||
|
|
||||||
private void onSentSucceed(SentBody body){}
|
//与服务端端开链接,重新连接
|
||||||
|
if(e instanceof CIMSessionDisableException)
|
||||||
@Override
|
{
|
||||||
public abstract void onMessageReceived(com.farsunset.cim.nio.mutual.Message message);
|
CIMPushManager.init(context);
|
||||||
@Override
|
}else
|
||||||
public abstract void onReplyReceived(ReplyBody body);
|
{
|
||||||
|
//发送失败 重新发送
|
||||||
public abstract void onNetworkChanged(NetworkInfo info);
|
CIMPushManager.sendRequest(context, body);
|
||||||
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void onSentSucceed(SentBody body){}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract void onMessageReceived(com.farsunset.cim.nio.mutual.Message message);
|
||||||
|
@Override
|
||||||
|
public abstract void onReplyReceived(ReplyBody body);
|
||||||
|
|
||||||
|
public abstract void onNetworkChanged(NetworkInfo info);
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user