Determine the target system model, realize the dynamic remote open PC folder

Next: Android Socket instance (six) KEY operation, that is, hot KEY operation

This more content:

Exquisite design of android interface, beautify the interface; Set up the service setting function, the modified service will be remembered, the next open will display the latest modified service IP, port; Realize the dynamic return key, in the dynamic access to the PC directory, you can realize the dynamic return to the upper level.

The server does not change

The KEY operation will be updated next time.

Android big changes

  • Using android. Content. SharedPreferences methods to modify the data again every time shut down the program opens again still save the modified value, in order to achieve the set service requirements.
  • Use string processing method to add dynamic return effect keys, each click to return to the last layer of the directory, in the outermost layer of the prompt has been in the outermost layer.
  • I designed the page to optimize the structure, beautify the design, and increase the aesthetic feeling to make the whole look more advanced. The design style here tends to be simple, which is designed by myself. I can use the parameters directly.

ICONS can be directly used by me or you can find them on the Internet. Most of them need PS processing to meet the requirements.

I’m going to give it here, because it’s the same color as the background, so you can’t see it, so you need to manually save it into your drawable.

menu1.png

search.png

MainActivity

package com.example.android_app;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    public static final String KEY_SERVER_ACK_MSG = "KEY_SERVER_ACK_MSG";
    private static final String KEY_IP="ip";
    private static final String KEY_PORT="port";
    private  Handler handler = null;
    EditText input;
    ListView lv;
    Button back;
    ImageView submit,opt_menu;
    SocketClient socketClient=null;
    String here;
    ArrayList<String> data;
    String ip;
    String port;
    TextView show_msg,here_msg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initdata();

        input=findViewById(R.id.dir);
        lv=findViewById(R.id.listview);
        submit=findViewById(R.id.submit);
        opt_menu=findViewById(R.id.opt_menu);
        back=findViewById(R.id.back);
        show_msg=findViewById(R.id.show_msg);
        here_msg=findViewById(R.id.here_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; }
        });


        opt_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(v);
            }
        });

        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:")){
                    here=s_input.substring(4);
                    socketClient=new SocketClient(ip,Integer.parseInt(port),handler);
                    socketClient.work(s_input);
                    here_msg.setText("当前位置:"+here);
                }else {
                    socketClient=new SocketClient(ip,Integer.parseInt(port),handler);
                    socketClient.work(s_input);
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int i=here.lastIndexOf("/");
                if (here.length()>4){
                    here=here.substring(0,i);
                    if (here.length()<4){
                        here=here+"/";
                    }
                    socketClient=new SocketClient(ip,Integer.parseInt(port),handler);
                    socketClient.work("dir:"+here);
                    here_msg.setText("当前位置:"+here);
                    show_msg.setText("成功返回上一层目录");
                }else {
                    show_msg.setText("已在最外层位置");
                    Toast.makeText(MainActivity.this,"已在最外层位置",Toast.LENGTH_SHORT).show();
                }
            }
        });

        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(ip,Integer.parseInt(port),handler);
                socketClient.work("dir:"+here);
                here_msg.setText("当前位置:"+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(ip,Integer.parseInt(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();
    }

    private void showPopupMenu(final View view) {
        final PopupMenu popupMenu = new PopupMenu(this, view);
        //menu 布局
        popupMenu.getMenuInflater().inflate(R.menu.opt_menu, popupMenu.getMenu());
        //点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.to_set:
                        showDialog_opt_set(MainActivity.this);
                        break;
                }
                return false;
            }
        });
        //关闭事件
        popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() {
            @Override
            public void onDismiss(PopupMenu menu) {
            }
        });
        //显示菜单,不要少了这一步
        popupMenu.show();
    }


    //ip,port设置初始化
    private void initdata() {
        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
        ip=sharedPreferences.getString(KEY_IP,"10.218.216.10");
        port=sharedPreferences.getString(KEY_PORT,"8019");
    }
    //ip,port保存设置
    public void saveset(String c_ip, String c_port){
        ip=c_ip;
        port=c_port;
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(KEY_IP,ip);
        editor.putString(KEY_PORT,port);
        editor.apply();
    }
    //ip,port修改
    public void showDialog_opt_set(final Context context){

        AlertDialog.Builder bl = new AlertDialog.Builder(context);
        bl.setTitle("服务设置");
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_view_opt_set, null, false);
        final EditText et1,et2;
        et1=v.findViewById(R.id.opt_set_ip);
        et2=v.findViewById(R.id.opt_set_port);
        et1.setText(ip);
        et2.setText(port);
        bl.setView(v);
        bl.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                saveset(et1.getText().toString(),et2.getText().toString());

                String s = "设置模式已关闭";
                Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
            }
        });
        bl.create().show();
    }
}



Copy the code

SocketClient without change

Activity_main.xml layout

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@color/bg_gray" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:padding="5dp" android:background="@color/bg_blue" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <ImageView android:layout_marginTop="14dp" android:layout_marginLeft="12dp" android:layout_marginRight="13dp" android:id="@+id/opt_menu" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/menu1"/> <EditText android:paddingLeft="6dp" android:layout_width="0dp" android:layout_weight="5"  android:layout_marginTop="6dp" android:height="36dp" android:background="@color/bg_white" android:layout_height="wrap_content" android:id="@+id/dir" android:text="dir:d://"/> <ImageView android:layout_marginTop="15dp" android:layout_marginLeft="10dp" android:layout_marginRight="12dp" android:id="@+id/submit" android:layout_width="20dp" android:layout_height="20dp" android:src="@drawable/search"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/show_msg" android:layout_width="0dp" android:layout_weight="5" android:layout_height="wrap_content" android:text="show_msg" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/back" android:text="back"/> </LinearLayout> <LinearLayout android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/bg_white" android:orientation="vertical"> <TextView android:paddingLeft="10dp" android:paddingTop="10dp" android:layout_width="wrap_content" android:layout_height="50dp" android:textSize="18sp" Android :textStyle="bold" Android :id="@+id/here_msg" Android :text=" Current location :" /> <VideoView Android: layout_width = "match_parent" android: layout_height = "0.5 dp" android: background = "# CCCCCC" / > < ListView android:padding="5dp" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listview" android:divider="#CCCCCC" android:dividerHeight="2px" /> </LinearLayout> </LinearLayout>Copy the code

Dialog_view_list. XML No changes (Layout)

Dialog_view_opt_set.xml (Layout added)

<? 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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" IP: <EditText android:layout_width="match_parent" Android :layout_height="wrap_content" Android :text="192.168.43.12" android:id="@+id/opt_set_ip"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Port: "/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:id="@+id/opt_set_port"/> </LinearLayout> </LinearLayout>Copy the code

Opt_menu. XML (menu new)

<? The XML version = "1.0" encoding = "utf-8"? > < menu XMLNS: android = "http://schemas.android.com/apk/res/android" > < item android: id = "@ + id/to_set android:" title = "Settings" / > </menu>Copy the code

Color.xml add color scheme (values)

<? The XML version = "1.0" encoding = "utf-8"? > <resources> <color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color> <color name="bg_blue">#3366ff</color> <color name="bg_white">#FFFFFF</color> <color name="bg_gray">#EEEEEE</color> </resources>Copy the code

Styles.xml modify the theme box (values)

<resources> <! -- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <! -- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources>Copy the code