Mercury-IM/app/src/main/java/org/mercury_im/messenger/ui/BindingActivity.java

56 lines
1.5 KiB
Java

package org.mercury_im.messenger.ui;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import androidx.appcompat.app.AppCompatActivity;
import org.mercury_im.messenger.service.XmppConnectionService;
public abstract class BindingActivity extends AppCompatActivity {
protected XmppConnectionService connectionService;
protected boolean bound;
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, XmppConnectionService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(serviceConnection);
bound = false;
}
protected void onServiceBound() {
}
protected void onServiceUnbound() {
}
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
XmppConnectionService.Binder binder = (XmppConnectionService.Binder) iBinder;
connectionService = binder.getService();
bound = true;
onServiceBound();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
bound = false;
onServiceUnbound();
}
};
}