Android interface design optimization, service Settings and dynamic return
This more content:
KEY operation, that is, hotkey operation, this more to achieve THE PC slide control and common shortcut keys as an example.
Virtual machines are not convenient to display functions. You are advised to use a real machine for debugging. The following figure shows virtual machine debugging
Server update
Added key operations, which parse key commands from Android and perform functions
The new KEY. Java
Implement PC side press the specified key method class
package lrz.data; import lrz.base.BaseOperator; import lrz.tool.VisualKeyMap; import java.awt.*; import java.util.ArrayList; public class KEY extends BaseOperator { private Robot robot; @Override public ArrayList<String> exe(String cmdBody) throws Exception { ArrayList<String> ackMsg = new ArrayList<String>(); robot=new Robot(); int splitIdx = cmdBody.indexOf(","); if (splitIdx < 1) { int splitIdx2 = cmdBody.indexOf("+"); if(splitIdx2<1){ singleKeyPress(cmdBody); }else{ simpleComboKeyPress(cmdBody); }}else{String keyPressStr=cmdBody. subString (0, splitIdx); String keyReleaseStr=cmdBody.substring(splitIdx+1); comboKeyPress(keyPressStr,keyReleaseStr); } ackmsg. add(" successfully run hotkey :"+cmdBody); return ackMsg; } private void simpleComboKeyPress(String keyPressStr){ String[] keyPressArray = keyPressStr.split("\\+"); For (int I =0; for(int I =0; for(int I =0; i<keyPressArray.length; i++){ int keycode = VisualKeyMap.getVisualKey(keyPressArray[i]); robot.keyPress(keycode); } for(int i=keyPressArray.length-1; i>=0; I -) {/ / release order button int keycode. = VisualKeyMap getVisualKey (keyPressArray [I]); robot.keyRelease(keycode); } } private void comboKeyPress(String keyPressStr, String keyReleaseStr) { // TODO Auto-generated method stub String[] keyPressArray = keyPressStr.split("\\+"); String[] keyReleaseArray = keyReleaseStr.split("\\+"); for(int i=0; i<keyPressArray.length; i++){ int keycode = VisualKeyMap.getVisualKey(keyPressArray[i]); robot.keyPress(keycode); } for(int i=0; i<keyReleaseArray.length; i++){ int keycode = VisualKeyMap.getVisualKey(keyReleaseArray[i]); robot.keyRelease(keycode); } } private void singleKeyPress(String cmdBody) { // TODO Auto-generated method stub int keycode = VisualKeyMap.getVisualKey(cmdBody); robot.keyPress(keycode); robot.keyRelease(keycode); }}Copy the code
New VisualKeyMap. Java
Store the shortcut keys we want to use, set here to call directly when using
package lrz.tool; import java.awt.event.KeyEvent; import java.util.HashMap; public class VisualKeyMap { private static HashMap<String, Integer> hashMap = new HashMap<String, Integer>(); private static final VisualKeyMap VISUAL_KEY_MAP = new VisualKeyMap(); private VisualKeyMap() { hashMap.put("VK_0", KeyEvent.VK_0); Hashmap. put("VK_TAB", keyevent.vk_tab); hashMap.put("VK_ALT", KeyEvent.VK_ALT); hashMap.put("VK_ESCAPE", KeyEvent.VK_ESCAPE); hashMap.put("VK_LEFT", KeyEvent.VK_LEFT); hashMap.put("VK_RIGHT", KeyEvent.VK_RIGHT); hashMap.put("VK_F5", KeyEvent.VK_F5); hashMap.put("VK_F4", KeyEvent.VK_F4); hashMap.put("LEFT", KeyEvent.BUTTON1_MASK); hashMap.put("RIGHT", KeyEvent.BUTTON3_MASK); hashMap.put("WIN", KeyEvent.VK_WINDOWS); hashMap.put("VK_D", KeyEvent.VK_D); hashMap.put("VK_CTRL", KeyEvent.VK_CONTROL); hashMap.put("VK_V", KeyEvent.VK_V); / / is omitted the need to increase the mapping operation} public static int getVisualKey (String key) {/ / call just VisualKeyMap getVisualKey (String key) to return hashMap.get(key.toUpperCase()); // Uppercase key}}Copy the code
Operator.java
Added operation invocation after key command resolution
if (cmdHead.equals("key")) { msgBackList = new KEY().exe(cmdBody); Msgbacklist.add (0,"key"); // add "ok" return msgBackList; }Copy the code
Android update
Add hotkey operation, slide control panel, generate key operation command when triggering event, and submit server through SocketClient
Added dialog_view_opt_do.xml layout
Hotkey operation panel layout
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" Android :layout_height="wrap_content" Android :id="@+id/vk_alt_tab" Android :text="ALT+TAB switch "/> <Button Android :layout_height="wrap_content" Android :id="@+id/vk_f5" Android :text="F5 refresh "/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/vk_win_d" Android :text="WIN+D display/hide desktop "/> <Button android:layout_width="match_parent" Android :layout_height="wrap_content" Android :id="@+ ID/vk_CTRl_F4 "Android :text="Ctrl+F4 close "/> </LinearLayout>Copy the code
Added dialog_view_opt_show.xml layout
Slide control panel layout
<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="match_parent" Android :layout_height="wrap_content" Android :id="@+id/vk_f5" Android :text=" show "/> <Button Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :id="@+id/vk_right" <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/vk_left" <Button android:layout_width="match_parent" Android :layout_height="wrap_content" Android :id="@+id/ vk_ESC "Android :text=" Exit playback "/> </LinearLayout>Copy the code
Update opt_menu. XML
Added hotkey panel and slide control panel options
<? The XML version = "1.0" encoding = "utf-8"? > < menu XMLNS: android = "http://schemas.android.com/apk/res/android" > < item android: id = "@ + id/to_do" android: title = "hotkey operation" / > <item Android :id="@+id/to_show" Android :title=" slide control "/> <item Android :id="@+ ID /to_set" Android :title=" setup "/> </menu>Copy the code
Update the SocketClient. Java
Public static int SERVER_MSG_KEY=4; // Returns the KEY operation
else if(status.equalsIgnoreCase("KEY")){
msgType=SERVER_MSG_KEY;
}
Copy the code
Update MainActivity. Java
Added hotkey and slide control options in function selection
case R.id.to_do:
showDialog_opt_do(MainActivity.this);
break;
case R.id.to_show:
show_Dialog_opt_show(MainActivity.this);
break;
Copy the code
Add the corresponding showDialog_opt_do and showDialog_opt_show methods to realize the corresponding panel pop-up
public void showDialog_opt_do(final Context context){ AlertDialog.Builder bl = new AlertDialog.Builder(context); Bl.settitle (" Control panel "); View v = LayoutInflater.from(context).inflate(R.layout.dialog_view_opt_do, null, false); final Button bt1=v.findViewById(R.id.vk_alt_tab); final Button bt2=v.findViewById(R.id.vk_f5); final Button bt3=v.findViewById(R.id.vk_win_d); final Button bt4=v.findViewById(R.id.vk_ctrl_f4); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_alt+vk_tab,vk_tab+vk_alt"); }}); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_f5"); }}); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:win+vk_d,vk_d+win"); }}); bt4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_ctrl+vk_f4,vk_f4+vk_ctrl"); }}); bl.setView(v); bl.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, Int which) {String s = "shortcut mode closed "; Toast.makeText(context,s,Toast.LENGTH_SHORT).show(); }}); bl.create().show(); } public void show_Dialog_opt_show(final Context context){ AlertDialog.Builder bl = new AlertDialog.Builder(context); Bl.settitle (" slide control "); View v = LayoutInflater.from(context).inflate(R.layout.dialog_view_opt_show, null, false); final Button bt1=v.findViewById(R.id.vk_f5); final Button bt2=v.findViewById(R.id.vk_right); final Button bt3=v.findViewById(R.id.vk_left); final Button bt4=v.findViewById(R.id.vk_esc); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_f5,vk_f5"); }}); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_right,vk_right"); }}); bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_left,vk_left"); }}); bt4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { socketClient=new SocketClient(ip,Integer.parseInt(port),handler); socketClient.work("key:vk_escape,vk_escape"); }}); bl.setView(v); bl.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, Int which) {String s = "Slide control mode turned off "; Toast.makeText(context,s,Toast.LENGTH_SHORT).show(); }}); bl.create().show(); }Copy the code
Add listener returned by key command in handler
Else if(fp== socketClient.server_msg_key){show_msg_settext (" execute KEY successfully "); }Copy the code
The key hot-key operation is implemented.
Useful have harvest ==> dot like dot like!