gpt4 book ai didi

java - 从服务到 Activity 的线程获取处理程序

转载 作者:行者123 更新时间:2023-12-01 13:57:37 24 4
gpt4 key购买 nike

情况:

  1. Activity 绑定(bind)到已启动的前台服务。
  2. 服务向 Activity 分发本地 Binder 。
  3. Activity 通过 getService() 调用获取对服务的引用。
  4. Activity 想要使用消息与服务中运行的线程直接进行通信。它从 Activity 中调用 mService.getThreadHandler() 方法。

问题:

  • 如何从当前正在运行的线程获取处理程序到 Activity Activity 中,以便我可以将消息直接发布到线程消息队列?

我不知道如何在服务中使用信使,我想从 Activity 端直接与服务中的线程进行通信。

编辑: Activity 通过调用如下内容来获取服务中线程本身的处理程序:

Activity 代码:

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;
}
}

服务代码:

private HandlerThread mServiceThread = new MyServiceThread();

public Handler getServiceHandler() {
return new Handler(mServiceThread.getLooper());
}

new Handler(mServiceThread.getLooper());返回一个新的Handler还是mServiceThread内相同的Handler?

编辑 2:使用接收消息的 serviceThread 更新服务代码。

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;
}
}

正确吗?

最佳答案

试试这个(我使用 Activity 来测试它,你将使用 Service):

protected void onCreate(Bundle savedInstanceState) {
ht = new HandlerThread("HT");
ht.start();
htHandler = new Handler(ht.getLooper(), htCallback);
mainHandler = new Handler(mainCallback);
for (int i = 0; i < 10; i++) {
htHandler.sendMessageDelayed(htHandler.obtainMessage(99, i, 0), i * 3000);
}
}

private HandlerThread ht;
private Handler htHandler;
private Handler mainHandler;

private Callback htCallback = new Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.d(TAG, "handleMessage **********************");
Log.d(TAG, "handleMessage " + msg);
Log.d(TAG, "handleMessage Thread: " + Thread.currentThread());
if (msg.arg1 == 4) {
Log.d(TAG, "handleMessage sending back to Main thread");
mainHandler.sendEmptyMessage(101);
}
return false;
}
};

private Callback mainCallback = new Callback() {
@Override
public boolean handleMessage(Message msg) {
Log.d(TAG, "handleMessage ########################");
Log.d(TAG, "handleMessage " + msg);
Log.d(TAG, "handleMessage Thread: " + Thread.currentThread());
Log.d(TAG, "handleMessage i'm quitting");
ht.quit();
return false;
}
};

关于java - 从服务到 Activity 的线程获取处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com