1:羽声新版本
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.xscm.moduleutil.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
|
||||
import com.xscm.moduleutil.service.IMConnectionService;
|
||||
|
||||
public class IMServiceManager {
|
||||
private static IMServiceManager instance;
|
||||
private boolean isServiceStarted = false;
|
||||
|
||||
private IMServiceManager() {
|
||||
}
|
||||
|
||||
public static synchronized IMServiceManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new IMServiceManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void startIMService(Context context) {
|
||||
if (isServiceStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent serviceIntent = new Intent(context, IMConnectionService.class);
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
context.startForegroundService(serviceIntent);
|
||||
} else {
|
||||
context.startService(serviceIntent);
|
||||
}
|
||||
isServiceStarted = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stopIMService(Context context) {
|
||||
if (!isServiceStarted) {
|
||||
return;
|
||||
}
|
||||
|
||||
Intent serviceIntent = new Intent(context, IMConnectionService.class);
|
||||
try {
|
||||
context.stopService(serviceIntent);
|
||||
isServiceStarted = false;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isServiceStarted() {
|
||||
return isServiceStarted;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user