这段代码如何封装?
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);
}
}
主要就是想通过广播形式来模拟按键操作!
封装教程不亲民 新手看的云里雾里
会封装的没时间搞
想封装的又不会搞
呼吁官方 抽一天时间 多出几个封装的例子
广大群众好照猫画虎的 封装下自己用的功能
顺便发上来 以后其他人在用这功能 不用重复造轮子
页:
[1]