124 lines
4.0 KiB
Java
124 lines
4.0 KiB
Java
package com.xscm.moduleutil.service;
|
|
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationManager;
|
|
import android.app.Service;
|
|
import android.content.Intent;
|
|
import android.os.Build;
|
|
import android.os.IBinder;
|
|
import android.util.Log;
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import com.blankj.utilcode.util.LogUtils;
|
|
import com.blankj.utilcode.util.ToastUtils;
|
|
import com.tencent.imsdk.v2.V2TIMManager;
|
|
import com.tencent.imsdk.v2.V2TIMSDKListener;
|
|
import com.tencent.imsdk.v2.V2TIMUserFullInfo;
|
|
import com.xscm.moduleutil.R;
|
|
import com.xscm.moduleutil.base.CommonAppContext;
|
|
import com.xscm.moduleutil.http.RetrofitClient;
|
|
|
|
public class IMConnectionService extends Service {
|
|
private static final String TAG = "IMConnectionService";
|
|
private static final int NOTIFICATION_ID = 2;
|
|
private static final String CHANNEL_ID = "im_connection_channel";
|
|
|
|
private final V2TIMSDKListener imSdkListener = new V2TIMSDKListener() {
|
|
@Override
|
|
public void onConnecting() {
|
|
Log.d(TAG, "IM connecting...");
|
|
}
|
|
|
|
@Override
|
|
public void onConnectSuccess() {//重连成功
|
|
Log.d(TAG, "IM connect success");
|
|
if (CommonAppContext.getInstance().playId != null) {
|
|
LogUtils.e("@@@", ""+CommonAppContext.getInstance().playId);
|
|
RetrofitClient.getInstance().roomUserReconnect(CommonAppContext.getInstance().playId);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onConnectFailed(int code, String error) {
|
|
Log.e(TAG, "IM connect failed, code: " + code + ", error: " + error);
|
|
}
|
|
|
|
@Override
|
|
public void onKickedOffline() {
|
|
Log.w(TAG, "IM kicked offline");
|
|
if (CommonAppContext.getInstance().playId != null) {
|
|
ToastUtils.showShort("您的账号已被挤下线");
|
|
try {
|
|
CommonAppContext.getInstance().clearLoginInfo();
|
|
} catch (ClassNotFoundException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public void onUserSigExpired() {
|
|
Log.w(TAG, "IM user sig expired");
|
|
}
|
|
|
|
@Override
|
|
public void onSelfInfoUpdated(V2TIMUserFullInfo info) {
|
|
Log.d(TAG, "IM self info updated");
|
|
}
|
|
|
|
};
|
|
|
|
@Override
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
startForegroundService();
|
|
V2TIMManager.getInstance().addIMSDKListener(imSdkListener);
|
|
Log.d(TAG, "IMConnectionService created and listener registered");
|
|
}
|
|
|
|
@Override
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
return START_STICKY; // 服务被杀死后会自动重启
|
|
}
|
|
|
|
@Nullable
|
|
@Override
|
|
public IBinder onBind(Intent intent) {
|
|
return null;
|
|
}
|
|
|
|
private void startForegroundService() {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
// 创建通知渠道
|
|
NotificationChannel channel = new NotificationChannel(
|
|
CHANNEL_ID,
|
|
"IM Connection Service",
|
|
NotificationManager.IMPORTANCE_LOW
|
|
);
|
|
NotificationManager manager = getSystemService(NotificationManager.class);
|
|
if (manager != null) {
|
|
manager.createNotificationChannel(channel);
|
|
}
|
|
|
|
// 创建通知
|
|
Notification notification = new Notification.Builder(this, CHANNEL_ID)
|
|
.setContentTitle("IM连接服务")
|
|
.setContentText("保持IM连接活跃")
|
|
.setSmallIcon(R.mipmap.default_avatar)
|
|
.setOngoing(true)
|
|
.build();
|
|
|
|
startForeground(NOTIFICATION_ID, notification);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onDestroy() {
|
|
super.onDestroy();
|
|
V2TIMManager.getInstance().removeIMSDKListener(imSdkListener);
|
|
Log.d(TAG, "IMConnectionService destroyed and listener unregistered");
|
|
}
|
|
} |