SharedPreferences实现设置地址保持不变(前提:app未卸载)

情景描述:

          通过访问本地电脑的ip(如地址:http://192.168.110.218:9999/fc?id=2&time=20190924)来下载并显示图片。因此要要实现的功能是:在用户登陆之前先把电脑的ip设置准确无误,不能每次登录都要去设置一次ip,,,我这里实现的功能是只要app没卸载,设置一次ip,次下次登陆不用设置就可访问(确保电脑IP是固定不变);

实现:

       一、创建java类 ConstData:

              private class ConstData{

      public static String FundusPhotoerIP = "112.45.108.111";

      public static String FundusPhotoerIP_KEY = "FundusPhotoerIP";

      public static String FundusPhotoerPort = "9999"; 

      public static String FundusPhotoerPort_Key = "FundusPhotoerPort";

      public static String getFundusPhotoerURL (){

          if (!ServerPort.equals("80")) {

                 return "HTTP://" + FundusPhotoerIP + ":" + FundusPhotoerPort+"/";
      }else {
      return "HTTP://" + FundusPhotoerIP + "/" ;
   }
    }
    public static void LoadSetting(Activity act){
    SharedPreferences sp;
    sp = act.getSharedPreferences("patient_data", Context.MODE_PRIVATE);

    FundusPhotoerIP = sp.getString(FundusPhotoerIP_KEY,FundusPhotoerIP);
    FundusPhotoerPort = sp.getString(FundusPhotoerPort_Key,FundusPhotoerPort);

      }

    }

    二、创建setActivity:

    public class setActivity entends Activity{

    private void saveURL(){

         editor.putString(ConstData.FundusPhotoerIP_KEY, eyeURL); //眼底url

         ConstData.FundusPhotoerIP = eyeURL;

         editor.commit();

         showTip("保存成功,请登录");

         startActivity(new Intent(this, LoginActivity.class));

      }

    }

  最后在登录里面调用:

    ConstData.LoadSetting(this);即可实现想要的功能!!!

 

 

如有疑问,欢迎评论!

        

 

原文地址:https://www.cnblogs.com/dmrbell/p/11577055.html