0
我正在開發使用Myo臂章的智能家居控制應用程序。我正在使用他們的SDK,但我運行的問題,我不知道如何解決。如何在特定線程中運行Myo SDK偵聽器
我有背景服務,它正在偵聽Myo姿勢,但即使SDK和設備偵聽器在後臺線程中初始化,它的事件(例如onPose)也會在主線程內引發。
有沒有辦法如何強制SDK在該後臺線程上引發事件?
服務代碼:
public class ListeningService extends Service {
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("MyoListener",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper, this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
return START_STICKY;
}
private final class ServiceHandler extends Handler {
private Context context;
public ServiceHandler(Looper looper, Context context) {
super(looper);
this.context = context;
}
@Override
public void handleMessage(Message msg) {
Hub hub = Hub.getInstance();
if (!hub.init(context, getPackageName())) {
showToast("Couldn't initialize Hub");
stopSelf();
return;
}
mListener = new MyoListener(context);
hub.addListener(mListener);
}
}
}