美文网首页Android
IP设置与还原功能(包括端口)

IP设置与还原功能(包括端口)

作者: 瑟闻风倾 | 来源:发表于2018-10-16 17:15 被阅读22次

1. HttpUrl类(管理Url地址)

/**
 * Created by liy on 2017-01-11.
 */
public class HttpUrl {
    public String defaultIP = "192.168.10.111:8080";//出厂IP地址
    public  String publicIP="";//当前设置的IP地址
    private Settings settings;
    private Activity activity;
    public  String publicUrl="";
 
    /****************************************所有的网络地址*******************************************************/
    public String loginUrl = "";
    public String logoutUrl = "";
  
    private static HttpUrl httpUrl;

    public static HttpUrl getHttpUrl(){
        if(httpUrl==null){
            httpUrl = new HttpUrl();
        }
        return httpUrl;
    }

    private HttpUrl(){
        settings = Settings.getInstance(activity);
        if (settings.getIP()!=""){
             publicIP=settings.getIP();
        }else{
            publicIP = defaultIP;
            settings.setIP(publicIP);//解决首次不显示默认IP
        }
        /*publicUrl="http://"+publicIP+":8080/jkpms";*/
        publicUrl="http://"+publicIP+"/jkpms";
        setAllUrl(publicUrl);
    }

    public void refleshIP(){
        settings = Settings.getInstance(activity);
        if (settings.getIP()!=""){
            publicIP=settings.getIP();
        }else{
            publicIP = defaultIP;
            settings.setIP(publicIP);
        }
        /*publicUrl="http://"+publicIP+":8080/jkpms";*/
        publicUrl="http://"+publicIP+"/jkpms";
        setAllUrl(publicUrl);
    }

    private void setAllUrl(String publicUrl){
       loginUrl = publicUrl+"//pmsAppMy/UserLogin.do";
       logoutUrl = publicUrl+"//pmsAppMy/logOutByUserId.do";
    }

}

备注:无参构造函数和刷新IP的函数代码此时一样,可根据需求进行对应的修改

2. Settings类(SharedPreferences管理)

/**
 * Created by liy on 2016-12-12.
 */
public class Settings {
    private static final String PREFERENCE = "settings";

    private SharedPreferences sharedPreferences;

    private static final String SETIP="set_IP";//上次设置的IP
  
  /**
   * 单例模式
   */
    private static Settings preferences;

    private Settings(Context context){
        sharedPreferences=context.getSharedPreferences(PREFERENCE,Context.MODE_PRIVATE);
    }

    public static Settings getInstance(Context context){
        if(preferences==null){
            preferences=new Settings(context);
        }
        return preferences;
    }

   /**
     * 修改IP和Port
     */
    public synchronized void setIP(String urlIP){
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString(SETIP,urlIP);
        editor.commit();
    }
    public synchronized String getIP(){
        return sharedPreferences.getString(SETIP,"");
    }
 
}

3. SystemSettingActivity

public class SystemSettingActivity extends FragmentActivity {
    private static final String TAG=SystemSettingActivity.class.getName();
    private Button submit;//IP确定
    private EditText IP_content1;//IP第1个字段输入框
    private EditText IP_content2;//IP第2个字段输入框
    private EditText IP_content3;//IP第3个字段输入框
    private EditText IP_content4;//IP第4个字段输入框
    private EditText port_number;//端口号
    private Settings settings;//设置
    private Button IP_settings;//IP还原
    private SystemSettingActivity settingActivity;

    private HttpUrl httpUrl;
    private LoggerUtil loggerUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loggerUtil = LoggerUtil.getLoggerUtil();
        setContentView(R.layout.fy_system_setting);
        settings = Settings.getInstance(this);
        initData();
        initViews();
        initListeners();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initData() {
        settingActivity = this;
        context=settingActivity.getApplicationContext();
        httpUrl=HttpUrl.getHttpUrl();
    }
  
    private void initViews() {
        IP_content1=(EditText)findViewById(R.id.input_IP1);
        IP_content2=(EditText)findViewById(R.id.input_IP2);
        IP_content3=(EditText)findViewById(R.id.input_IP3);
        IP_content4=(EditText)findViewById(R.id.input_IP4);
        port_number = (EditText)findViewById(R.id.input_port);
        submit=(Button)findViewById(R.id.change_IP);
        IP_settings=(Button)findViewById(R.id.IP_settings);

        if (settings.getIP()!=""){
            String ip_port=settings.getIP();
            String[] ipPortArray = ip_port.split(":");
            if (ipPortArray.length==2) {
                String IP_content=ipPortArray[0];
                String[] IPArray=IP_content.split("\\.");
                if (IPArray.length==4){
                    IP_content1.setText(IPArray[0]);
                    IP_content2.setText(IPArray[1]);
                    IP_content3.setText(IPArray[2]);
                    IP_content4.setText(IPArray[3]);
                }
                port_number.setText(ipPortArray[1]);
            }
        }
    }

    private void initListeners() {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String IP1=IP_content1.getText().toString();
                String IP2=IP_content2.getText().toString();
                String IP3=IP_content3.getText().toString();
                String IP4=IP_content4.getText().toString();
                String port = port_number.getText().toString();
                String adressIP=IP1+"."+IP2+"."+IP3+"."+IP4+":"+port;

                if (!adressIP.equals("")) {
                    if (!IP1.equals("") && !IP2.equals("") && !IP3.equals("") && !IP4.equals("")&& !port.equals("")) {
                        if(adressIP.equals(settings.getIP())){
                            return;
                        }else {
                            settings.setIP(adressIP);
                            httpUrl.refleshIP();
                            Toast.makeText(settingActivity, getString(R.string.change_IP), Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(settingActivity, getString(R.string.empty_IP), Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(settingActivity, getString(R.string.empty_IP), Toast.LENGTH_SHORT).show();
                }
            }
        });

        IP_settings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(settings.getIP().equals(httpUrl.defaultIP)){
                    Toast.makeText(settingActivity,getString(R.string.default_ip_port), Toast.LENGTH_SHORT).show();
                }else {
                    warningIpRestore();//IP还原警告框
                }
            }
        });

    }

    //IP还原警告框
    private void warningIpRestore() {
        new AlertDialog.Builder(this)
                .setTitle(getString(R.string.IpRestore_title))
                .setMessage(R.string.IpRestore_Message)
                .setPositiveButton(R.string.button_sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        settings.setIP(httpUrl.defaultIP);
                        initViews();
                        httpUrl.refleshIP();//用于刷新IP,避免没有点击确定时虽然显示为默认但并没有应用到所用接口
                        Toast.makeText(settingActivity, getString(R.string.change_IP), Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();
    }

}

4. fy_system_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请输入要修改的IP和端口号:"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:id="@+id/input_IP1"
                android:gravity="center"
                android:layout_weight="3"
                android:maxLength="3"
                android:inputType="number"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="."/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:id="@+id/input_IP2"
                android:gravity="center"
                android:layout_weight="3"
                android:maxLength="3"
                android:inputType="number"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="."/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:id="@+id/input_IP3"
                android:gravity="center"
                android:layout_weight="3"
                android:maxLength="3"
                android:inputType="number"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="."/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:id="@+id/input_IP4"
                android:gravity="center"
                android:layout_weight="3"
                android:maxLength="3"
                android:inputType="number"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=":"/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:id="@+id/input_port"
                android:gravity="center"
                android:layout_weight="4"
                android:maxLength="4"
                android:inputType="number"/>
        </LinearLayout>
        <Button
            android:id="@+id/change_IP"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="确定"/>
    </LinearLayout>

    <View style="@style/list_item_cell_seperator_layout"/>
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="#808080"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <Button
            android:id="@+id/IP_settings"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="IP还原"/>
    </LinearLayout>

</LinearLayout>

相关文章

  • IP设置与还原功能(包括端口)

    1. HttpUrl类(管理Url地址) 备注:无参构造函数和刷新IP的函数代码此时一样,可根据需求进行对应的修改...

  • GitLab维护指南

    GitLab安装和升级 服务器迁移 修改服务端口号 修改HTTP连接方式中的IP和端口 邮件通知功能设置 GitL...

  • redis主从

    一.设置slave node slaveof ip port (设置主节点的ip和端口)slave-read-on...

  • C++ 实现智能大厦自动报警系统,源码实例讲解

    VC++ 智能大厦自动报警系统,功能包括布防参数设置、处警参数设置、报警动作设置、通讯端口设置、日志管理、系统管理...

  • 移动端fiddle Https抓包

    1.查找本机的IP地址,设置fiddle的端口号 2.在手机访问本机IP:端口,安装证书 3.fiddle设置允许...

  • C++制作智能自动报警系统,比人类保安还智能,附源码讲解!

    C++智能大厦自动报警系统,功能包括布防参数设置、处警参数设置、报警动作设置、通讯端口设置、日志管理、系统管理等功...

  • mac 终端配置代理

    1.首先获取自己的代理IP 和 端口。可以在网络设置,高级设置中查看。 2.指定代理ip 端口,下面这种方式只在当...

  • 网络总结

    开启超级终端 enbleconfig terminal 开启端口/接口 no shutdown 设置IP地址 ip...

  • 树莓派

    1.设置静态IP 确认端口设置IP、掩码、路由和DNS 最后执行 2.设置wifi 设置树莓派的wifi连接 最后执行

  • mac爪包工具Charles

    1、手动设置iphone wifi 的http 代理设置为 电脑端的ip地址 端口改为chrles的默认端口888...

网友评论

    本文标题:IP设置与还原功能(包括端口)

    本文链接:https://www.haomeiwen.com/subject/pgvhzftx.html