zgodnie z Androidową poprawnością należało bu to zrobić tak:
kod przykładowej usługi i zmienna startTime której zawartość chcesz uzyskać:
public class MyService extends Service {
public static final int REGISTER_CLIENT = 0x1;
public static final int REMOVE_CLIENT = 0x2;
public static final int START_TIME = 0x3;
private final ArrayList<Messenger> mClients = new ArrayList<Messenger>();
private final Messenger mMessenger = new Messenger(new ReciveHandler());
private long startTime = 0;
@Override
public IBinder onBind(Intent arg0) {
return mMessenger.getBinder();
}
private void sendHandler(Message message) {
if (!mClients.isEmpty()) {
message.replyTo = mMessenger;
for (int i = (mClients.size() - 1); i >= 0; i--) {
try {
mClients.get(i).send(message);
} catch (RemoteException e) {
Log.w("warn", "Remote Exception");
mClients.remove(i);
}
}
}
}
private class ReciveHandler extends Handler {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case REGISTER_CLIENT:
mClients.add(message.replyTo);
break;
case REMOVE_CLIENT:
mClients.remove(message.replyTo);
break;
case START_TIME:
sendHandler(Message.obtain(null, START_TIME, startTime));
break;
default:
super.handleMessage(message);
}
}
}
}
oraz kod przykładowej aktywności :
public class MyActivity extends Activity {
private Messenger mService = null;
private final Messenger mMessenger = new Messenger(new ReciveHandler());
private final Connection mConnection = new Connection();
private boolean isBound = false;
private long startTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(getServiceIntent());
}
@Override
protected void onResume() {
super.onResume();
doBindService();
}
@Override
protected void onPause() {
super.onPause();
doUnbindService();
}
private Intent getServiceIntent() {
return new Intent(MyActivity.this, MyService.class);
}
private class Connection implements ServiceConnection {
public void onServiceConnected(ComponentName className, IBinder service) {
mService = new Messenger(service);
sendHandler(Message.obtain(null, MyService.REGISTER_CLIENT));
}
public void onServiceDisconnected(ComponentName className) {
mService = null;
}
}
private void doBindService() {
bindService(getServiceIntent(), mConnection, 0);
isBound = true;
}
private void doUnbindService() {
if (isBound) {
if (mService != null) {
sendHandler(Message.obtain(null, MyService.REMOVE_CLIENT));
}
unbindService(mConnection);
isBound = false;
}
}
private void sendHandler(Message message) {
try {
message.replyTo = mMessenger;
mService.send(message);
} catch (RemoteException e) {
Log.i("Info", "Remote Exception");
}
}
private class ReciveHandler extends Handler {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case MyService.START_TIME:
startTime = (long) message.obj;
break;
default:
super.handleMessage(message);
}
}
}
}
również ze zmienną startTime, aby "zsynchronizować" obie zmienne należy wykonać:
sendHandler(Message.obtain(null, MyService.START_TIME));
metoda może zawiła na pierwszy rzut oka, ale niezwykle pomocna na przyszłość do pracy z Usługami
dzięki tej metodzie startTime może się auto "synchronizować" więc możesz zapomnieć o poleceniach itd..