Pobieranie przewodnika z wątku z usługi do działania
Sytuacja:
aktywność wiąże się z uruchomieniem usługi pierwszego planu.serwis rozdaje lokalne spoiwo aktywności.Działanie pobiera odwołanie do usługi za pośrednictwem wywołania getService ().aktywność chce się komunikowaćbezpośrednio z wątkiem działającym w usłudze za pomocą wiadomości. Wywołuje metodę mService.getThreadHandler () z działania.Problem:
jak uzyskać Handler z bieżącego wątku do aktywnego działania, aby móc wysyłać wiadomości bezpośrednio do kolejki wątków?Nie chcę korzystać z komunikatora w ramach usługi, chcę bezpośrednio komunikować się z wątkiem usługi od strony aktywności.
Edytować: działanie pobiera obsługę samego wątku w usłudze, wywołując coś takiego:
Kod aktywności:
Handler mServiceThreadHandler;
ServiceConnection mServiceConnection;
public void onStart() {
if (!bindService(new Intent(this, MainService.class), mServiceConnection, Context.BIND_AUTO_CREATE))
{
Log.e(TAG, "Client did not bind to Service!");
}
super.onStart();
}
public class MyLocalServiceConnection implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocalBinder binder = (LocalBinder) service;
mService = (MainService) binder.getService();
// the handler of the service is actually a handler of a thread
// within the service, and is set automatically within the binding
// activity when binding to the service. That way you have a direct
// "connection" with the message queue of the thread instead of
// a message queue in the service itself (main thread of service)
mServiceThreadHandler = mService.getServiceHandler();
if (mServiceThreadHandler == null) {
Log.e(TAG, "Service handler is NULL");
}
mBoundedToService = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceThreadHandler = null;
mBoundedToService = false;
}
}
Kod usługi:
private HandlerThread mServiceThread = new MyServiceThread();
public Handler getServiceHandler() {
return new Handler(mServiceThread.getLooper());
}
Czy tonowy Handler (mServiceThread.getLooper ()); zwrócić nowy Handler lub ten sam Handler w mServiceThread?
Edytuj 2: aktualizacja kodu usługi za pomocą serviceThread, który odbiera wiadomości.
public class MyService extends Service {
private MyHandlerThread serviceThread = new MyHandlerThread("serviceThread");
serviceThread.start();
public Handler getServiceHandler() {
// hand out the same handler of the service thread for using it in an activity!
// serviceThread.getLooper() is the current looper of the thread
// serviceThread is the 'this' which handles the messages (see MyHandlerThread)
return new Handler(serviceThread.getLooper(), serviceThread);
}
// do stuff in Service
}
public class MyHandlerThread extends HandlerThread implements android.os.Handler.Callback {
public MyHandlerThread(String name) {
super(name);
}
public MyHandlerThread(String name, int priority) {
super(name, priority);
}
@Override
public boolean handleMessage(Message msg) {
// TODO define your own message handling here.
return false;
}
}
Poprawny?