|
- import android.content.Context;
- import android.content.Intent;
- import android.view.KeyEvent;
- public class TVServer {
- private ServerSocket serverSocket;
- private Context context;
- public TVServer(Context context) {
- this.context = context;
- try {
- serverSocket = new ServerSocket(8080);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void startServer() {
- new Thread(() -> {
- while (true) {
- try {
- Socket clientSocket = serverSocket.accept();
- BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
- String command = in.readLine();
- handleCommand(command);
- clientSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- private void handleCommand(String command) {
- switch (command) {
- case Command.UP:
- sendKeyEvent(KeyEvent.KEYCODE_DPAD_UP);
- break;
- case Command.DOWN:
- sendKeyEvent(KeyEvent.KEYCODE_DPAD_DOWN);
- break;
- case Command.LEFT:
- sendKeyEvent(KeyEvent.KEYCODE_DPAD_LEFT);
- break;
- case Command.RIGHT:
- sendKeyEvent(KeyEvent.KEYCODE_DPAD_RIGHT);
- break;
- case Command.HOME:
- sendKeyEvent(KeyEvent.KEYCODE_HOME);
- break;
- case Command.BACK:
- sendKeyEvent(KeyEvent.KEYCODE_BACK);
- break;
- }
- }
- private void sendKeyEvent(int keyCode) {
- Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
- intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, keyCode));
- context.sendBroadcast(intent);
- intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, keyCode));
- context.sendBroadcast(intent);
- }
- }
复制代码 主要就是想通过广播形式来模拟按键操作!
|
|