上一篇:安卓socket实例(三)实现OPN、DIR操作,优化架构
本更内容:通过数据流图浅谈底层数据变换过程,确定目标系统模型。
并在动态访问服务端目录的基础上,当长按某一listview中的view触发事件,弹出AlertDialog菜单,点击PC打开则向服务器提交处理好的OPN操作命令,实现远程动态打开PC文件夹。
目标系统分析:
MainActivity添加长按listview中的view的监听器,当触发时弹出设计好的AlertDialog,并在其中添加布局,当点击PC打开按钮时,安卓端会根据此事件产生特定命令,并通过SocketClient将此命令写入输入流。
此时阻塞式的服务端ServerSocket会收到传入的数据流信息,阻塞状态解除,调用SocketMsg来解析数据转换为可以使用的字符串数组,然后将此数据传入Operator调度命令实现功能。
命令执行后返回结果,此结果又通过SocketMsg写入数据流返回给安卓端。
SocketClient会接受此返回信息的数据流,并将此返回结果写入handler发送给MainActivity的hanlder监听器,至此整个架构流程实现。
idea服务端无更改
安卓端
MainActivity添加listview长按监听器以及弹窗实现
package com.example.android_app;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public static final String KEY_SERVER_ACK_MSG = "KEY_SERVER_ACK_MSG";
private Handler handler = null;
EditText url,way,input;
ListView lv;
Button submit;
SocketClient socketClient=null;
String here;
ArrayList<String> data;
int port;
TextView show_msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url=findViewById(R.id.url);
way=findViewById(R.id.way);
input=findViewById(R.id.dir);
lv=findViewById(R.id.listview);
submit=findViewById(R.id.submit);
show_msg=findViewById(R.id.show_msg);
handler=new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(@NonNull Message msg) {
int fp=msg.arg2;
Bundle data_bundle = msg.getData();
if(fp==SocketClient.SERVER_MSG_DIR){
data=data_bundle.getStringArrayList(KEY_SERVER_ACK_MSG);
data=dataMaker();
printAdapter(data);
show_msg.setText("成功执行DIR操作");
}else if(fp==SocketClient.SERVER_MSG_OPN){
show_msg.setText("成功执行OPN操作");
}
return false; }
});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s_input=input.getText().toString();
if (s_input.substring(0,4).equals("dir:")){
port=Integer.parseInt(way.getText().toString());
here=s_input.substring(4);
socketClient=new SocketClient(url.getText().toString(),port,handler);
socketClient.work(s_input);
}else {
socketClient=new SocketClient(url.getText().toString(),port,handler);
socketClient.work(s_input);
}
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s_here=here;
int s_here_p=s_here.lastIndexOf("/");
if (s_here_p==s_here.length()-1){
here=here.substring(0,s_here_p);
}
here=here+"/"+data.get(position);
socketClient=new SocketClient(url.getText().toString(),port,handler);
socketClient.work("dir:"+here);
}
});
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
showDialog_list_main(MainActivity.this,data.get(position));
return false;
}
});
}
private ArrayList<String> dataMaker() {
ArrayList<String> dataResult=new ArrayList<>();
int i=data.size();
for (int j = 0; j <i ; j++) {
String str=data.get(j);
str=str.substring(0,str.indexOf(">"));
dataResult.add(str);
}
return dataResult;
}
private void printAdapter(ArrayList<String> data) {
ArrayAdapter<String> arrayAdapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,data);
lv.setAdapter(arrayAdapter);
}
public void showDialog_list_main(final Context context, final String opn_name) {
AlertDialog.Builder bl = new AlertDialog.Builder(context);
bl.setTitle("文件操作");
View v = LayoutInflater.from(context).inflate(R.layout.dialog_view_list, null, false);
String s_opn=here;
int s_opn_p=s_opn.lastIndexOf("/");
if (s_opn_p==s_opn.length()-1){
s_opn=s_opn.substring(0,s_opn_p);
}
System.out.println(s_opn);
final String opn_dir="opn:"+s_opn+"/"+opn_name;
final Button bt1=v.findViewById(R.id.opn);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
socketClient=new SocketClient(url.getText().toString(),port,handler);
socketClient.work(opn_dir);
String ss = opn_name+"即将在PC端打开";
Toast.makeText(context,ss,Toast.LENGTH_SHORT).show();
}
});
bl.setView(v);
bl.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
bl.create().show();
}
}
复制代码
新增dialog_view_list.xml
<?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/opn"
android:text="PC打开"/>
<!-- <Button-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content"-->
<!-- android:text="代实现"/>-->
</LinearLayout>
复制代码
创作不易,如果觉得有帮助,给个小攒攒叭ε=ε=ε=( ̄▽ ̄)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END