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		startService(new Intent(this, XmppConnectionService.class));
35		super.onStart();
36		if (!xmppConnectionServiceBound) {
37			Intent intent = new Intent(this, XmppConnectionService.class);
38			bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
39		}
40	}
41	
42	@Override
43	protected void onStop() {
44		super.onStop();
45		if (xmppConnectionServiceBound) {
46			unbindService(mConnection);
47			xmppConnectionServiceBound = false;
48		}
49	}
50	
51	abstract void onBackendConnected();
52}