【发布时间】:2017-04-17 15:57:54
【问题描述】:
我需要在我的应用程序中无限期地从外部硬件监听key-presses,为此我想创建一个bound service,只要我的应用程序正在运行,它就可以工作。
问题是,这项工作需要在单独的thread 上完成,到目前为止,我只能让我的服务将预设值传回我的activity。经过一番研究,我了解到这是因为服务默认运行在 ui thread 上。
...但是如果我在服务中运行我的线程,我不知道如何将处理后的结果发送回我的活动。
当我尝试直接在活动中执行此过程时,我会在处理按键后立即使用runOnUiThread() 方法更新文本视图,但因为我现在使用的是service无权访问该方法。
我的DeviceService 类使用 Handler 和 Messenger 来接收来自客户端活动的响应:
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case (MSG_START_REST):
// This starts a worker thread.
// I DON'T KNOW HOW TO GET THE RESULT BACK TO THE UI THREAD
Log.v(TAG, "Starting listening for keypad");
startREST(msg);
// THIS IS WHERE I'M HAVING TROUBLE
// I want to set this to the result of the above call...
String keyPressed = "ice cream";
try {
// Response
Message keyResponse = Message.obtain(null, KEYPRESS);
Bundle bundle = new Bundle();
bundle.putString("keyPressed", keyPressed);
keyResponse.setData(bundle);
msg.replyTo.send(keyResponse);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
// This works.
case (TO_UPPERCASE):
try {
// Incoming data
String data = msg.getData().getString("data");
// Response
Message resp = Message.obtain(null, TO_UPPERCASE_RESPONSE);
Bundle bResp = new Bundle();
bResp.putString("respData", data.toUpperCase());
resp.setData(bResp);
msg.replyTo.send(resp);
} catch (RemoteException e) {
e.printStackTrace();
}
break;
default:
super.handleMessage(msg);
}
}
// Target we publish for clients to send messages to IncomingHandler.
final Messenger mMessenger = new Messenger(new IncomingHandler());
我不会显示我所有的线程处理(它工作正常),但这里是它完成并得到它的结果的地方。它传递了一行 json,从中提取 keycode。
同样,我的问题是我不知道如何将此键码恢复到我的活动中。
private void sendKeyboardEvent(final String keyboardEvent) {
Log.v(TAG, "Keyboard event : " + keyboardEvent);
// Parse json to find keycode
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(keyboardEvent);
// THIS IS THE KEYCODE. IT WILL BE SOMETHING LIKE "30" or "31"
String keycode = jsonObject.getString("pressedKeycodes");
Log.v(TAG, "Keycode : " + keycode);
} catch (JSONException e) {
e.printStackTrace();
}
}
我的MainActivity 也有一个 Handler 和 Messenger 来解释来自服务的响应。
// Handler of incoming messages from clients.
class ResponseHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case (DeviceService.KEYPRESS):
String result4 = msg.getData().getString("keyPressed");
Toast.makeText(getApplicationContext(), "A key was pressed! -"+ result4, Toast.LENGTH_SHORT).show();
break;
case (DeviceService.TO_UPPERCASE_RESPONSE):
String result = msg.getData().getString("respData");
TextView tvResult = (TextView) findViewById(R.id.textView_result);
tvResult.setText(result);
break;
default:
super.handleMessage(msg);
}
}
}
这是我向服务发送调用的地方。这似乎也可以正常工作。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnResult = (Button) findViewById(R.id.btnResult);
btnResult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String val = "hello";
Message msg = Message.obtain(null, DeviceService.TO_UPPERCASE);
msg.replyTo = new Messenger(new ResponseHandler());
Bundle b = new Bundle();
b.putString("data", val);
msg.setData(b);
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
Button btnStart = (Button) findViewById(R.id.btn_start_keypad_listener);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Message msg = Message.obtain(null, DeviceService.MSG_START_REST);
msg.replyTo = new Messenger(new ResponseHandler());
try {
mService.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
Tldr,我需要将运行在 MainActivity 之外的线程的结果发送回我的 MainActivity 和 UI 线程。
请帮忙?
【问题讨论】:
-
如果您需要执行特定操作,请改用 IntentService
-
@LunarWatcher 这看起来是一个很有前途的解决方案。我可以以同样的方式使用 bindind 吗?该文档有一个 onBind() 方法,但因为我现在将使用意图,所以它的处理方式似乎不同