В настоящее время я использую приложение для видеовызовов. Когда я закрываю свое приложение, сервис не работает. Как я могу сохранить службу ..?
Пожалуйста, помогите мне.
Служба вызова mainActivity и она были уничтожены, передавая значения параметров в CallService.class.
Таким образом, CallService.class будет вызываться из метода start.
public static void start(Context context, QBUser qbUser, PendingIntent pendingIntent) {
Log.i(TAG,"CallService start");
Intent intent = new Intent(context, CallService.class);
intent.putExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_LOGIN);
intent.putExtra(Consts.EXTRA_QB_USER, qbUser);
intent.putExtra(Consts.EXTRA_PENDING_INTENT, pendingIntent);
context.startService(intent);
}
public static void start(Context context, QBUser qbUser) {
start(context, qbUser, null);
}
@Override
public void onCreate() {
super.onCreate();
//챗 서비스 만들기
createChatService();
Log.d(TAG, " CallService onCreate()");
}
/** 서비스가 시작되면 이 메소드가 호출된다. **/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "CallService onStartCommand");
//인텐트로 받아온 값들을 할당
parseIntentExtras(intent);
//적절한 액션으로 시작한다. (로그인이냐, 로그아웃이냐)
startSuitableActions();
//시스템에 의해 종료가 되어도 서비스는 물론, 전달되었던 intent 값까지 모두 유지시켜주는 값
return START_REDELIVER_INTENT;
}
private void parseIntentExtras(Intent intent) {
if (intent != null && intent.getExtras() != null) {
currentCommand = intent.getIntExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_NOT_FOUND);
pendingIntent = intent.getParcelableExtra(Consts.EXTRA_PENDING_INTENT);
currentUser = (QBUser) intent.getSerializableExtra(Consts.EXTRA_QB_USER);
}
}
private void startSuitableActions() {
if (currentCommand == Consts.COMMAND_LOGIN) {
Log.i(TAG,"CallService startSuitableActions");
startLoginToChat();
} else if (currentCommand == Consts.COMMAND_LOGOUT) {
Log.i(TAG,"CallService Logout");
logout();
}
}
/** 채팅 서비스 생성하기 **/
private void createChatService() {
if (chatService == null) {
Log.i(TAG,"CallService createChatService");
QBTcpConfigurationBuilder configurationBuilder = new QBTcpConfigurationBuilder();
configurationBuilder.setSocketTimeout(0);
QBChatService.setConnectionFabric(new QBTcpChatConnectionFabric(configurationBuilder));
QBChatService.setDebugEnabled(true);
chatService = QBChatService.getInstance();
}
}
/** 로그인 들어가기 전 **/
private void startLoginToChat() {
if (!chatService.isLoggedIn()) {
Log.i(TAG,"CallService startLoginToChat");
loginToChat(currentUser);
} else {
sendResultToActivity(true, null);
Log.i(TAG,"CallService sendResultToActivity");
}
}
/** 로그인 **/
private void loginToChat(QBUser qbUser) {
chatService.login(qbUser, new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
Log.d(TAG, "CallService loginToChat onSuccess");
startActionsOnSuccessLogin();
}
@Override
public void onError(QBResponseException e) {
Log.d(TAG, "CallService loginToChat onError " + e.getMessage());
sendResultToActivity(false, e.getMessage() != null
? e.getMessage()
: "Login error");
}
});
}
private void startActionsOnSuccessLogin() {
initPingListener();
initQBRTCClient();
sendResultToActivity(true, null);
}
private void initPingListener() {
ChatPingAlarmManager.onCreate(this);
Log.d(TAG, "CallService initPingListenerPing");
ChatPingAlarmManager.getInstanceFor().addPingListener(new PingFailedListener() {
@Override
public void pingFailed() {
Log.d(TAG, "CallService initPingListenerPing chat server failed");
}
});
}
private void initQBRTCClient() {
rtcClient = QBRTCClient.getInstance(getApplicationContext());
Log.d(TAG, "CallService initQBRTCClient");
// Add signalling manager
chatService.getVideoChatWebRTCSignalingManager().addSignalingManagerListener(new QBVideoChatSignalingManagerListener() {
@Override
public void signalingCreated(QBSignaling qbSignaling, boolean createdLocally) {
if (!createdLocally) {
rtcClient.addSignaling((QBWebRTCSignaling) qbSignaling);
}
}
});
// Configure
QBRTCConfig.setDebugEnabled(true);
SettingsUtil.configRTCTimers(CallService.this);
// Add service as callback to RTCClient
rtcClient.addSessionCallbacksListener(WebRtcSessionManager.getInstance(this));
rtcClient.prepareToProcessCalls();
}
private void sendResultToActivity(boolean isSuccess, String errorMessage) {
if (pendingIntent != null) {
Log.d(TAG, "CallService sendResultToActivity()");
try {
Intent intent = new Intent();
intent.putExtra(Consts.EXTRA_LOGIN_RESULT, isSuccess);
intent.putExtra(Consts.EXTRA_LOGIN_ERROR_MESSAGE, errorMessage);
pendingIntent.send(CallService.this, Consts.EXTRA_LOGIN_RESULT_CODE, intent);
} catch (PendingIntent.CanceledException e) {
String errorMessageSendingResult = e.getMessage();
Log.d(TAG, errorMessageSendingResult != null
? errorMessageSendingResult
: "Error sending result to activity");
}
}
}
public static void logout(Context context) {
Intent intent = new Intent(context, CallService.class);
intent.putExtra(Consts.EXTRA_COMMAND_TO_SERVICE, Consts.COMMAND_LOGOUT);
context.startService(intent);
}
private void logout() {
destroyRtcClientAndChat();
}
private void destroyRtcClientAndChat() {
if (rtcClient != null) {
rtcClient.destroy();
}
ChatPingAlarmManager.onDestroy();
if (chatService != null) {
chatService.logout(new QBEntityCallback<Void>() {
@Override
public void onSuccess(Void aVoid, Bundle bundle) {
chatService.destroy();
}
@Override
public void onError(QBResponseException e) {
Log.d(TAG, "logout onError " + e.getMessage());
chatService.destroy();
}
});
}
stopSelf();
}
@Override
public void onDestroy() {
Log.d(TAG, "CallService Service onDestroy()");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "CallService Service onBind)");
return null;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.d(TAG, "CallService Service onTaskRemoved()");
super.onTaskRemoved(rootIntent);
destroyRtcClientAndChat();
}
}