XmppActivity.java

 1package de.gultsch.chat.ui;
 2
 3import de.gultsch.chat.services.XmppConnectionService;
 4import de.gultsch.chat.services.XmppConnectionService.XmppConnectionBinder;
 5import android.app.Activity;
 6import android.content.ComponentName;
 7import android.content.Context;
 8import android.content.Intent;
 9import android.content.ServiceConnection;
10import android.os.IBinder;
11
12public abstract class XmppActivity extends Activity {
13	public XmppConnectionService xmppConnectionService;
14	public boolean xmppConnectionServiceBound = false;
15	protected boolean handledViewIntent = false;
16	protected ServiceConnection mConnection = new ServiceConnection() {
17
18		@Override
19		public void onServiceConnected(ComponentName className, IBinder service) {
20			XmppConnectionBinder binder = (XmppConnectionBinder) service;
21			xmppConnectionService = binder.getService();
22			xmppConnectionServiceBound = true;
23			onBackendConnected();
24		}
25
26		@Override
27		public void onServiceDisconnected(ComponentName arg0) {
28			xmppConnectionServiceBound = false;
29		}
30	};
31	
32	@Override
33	protected void onStart() {
34		super.onStart();
35		if (!xmppConnectionServiceBound) {
36			Intent intent = new Intent(this, XmppConnectionService.class);
37			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
38		}
39	}
40	
41	@Override
42	protected void onStop() {
43		super.onStop();
44		if (xmppConnectionServiceBound) {
45			unbindService(mConnection);
46			xmppConnectionServiceBound = false;
47		}
48	}
49	
50	abstract void onBackendConnected();
51}