This article describes the Android Observer Mode. Share it for your reference. The specific analysis is as follows:
1. Environment:
Host:WIN8
Development Environment: Eclipse
2. Description:
1. Open the xml file in the SD card. If it does not exist, create a new one and write it to the default configuration.
2. Read the xml file
3.Config_Info.java is the configuration information data structure
4. IF_Config.java is the access interface of the configuration class. Other classes can directly obtain configuration information through this interface.
5.IF_Subject_Config.java is the observer mode target class interface
6.IF_Observer_Config.java is the observer class interface
For configuration classes, complete 1 and 2 tasks, and are also the target class of observer mode. Once the configuration information changes, the observer class is notified.
Observer for observer mode
Loosely coupled design can be achieved through the access interface + observer mode.
3. XML file format:
<?xml version="1.0" encoding="UTF-8" standalone="true"?> -<config> <title>Remote video meeting system</title> <local_port>12600</local_port> <schedule_service_ip>10.58.1.59</schedule_service_ip> <schedule_service_port>12601</schedule_service_port> </config>
4. Source code:
Config_Info.java:
/** * Configuration information data type * New date: 2014/12/8 by jdh */ package ; public class Config_Info { //title public String title; //Native IP public String local_ip; //Native port public int local_port; //Dispatch server ip public String schedule_server_ip; //Scheduling server port public int schedule_server_port; }
IF_Config.java:
/** * Interface: configuration class, read and write * New date: 2014/12/8 by jdh */ package ; public interface IF_Config { public Config_Info get_config_info(); }
IF_Subject_Config.java:
/** * Interface: Configuration class, Observer mode: Target * New creation time: 2014/12/8 by jdh */ package ; public interface IF_Subject_Config { public void register_observer(IF_Observer_Config observer); public void remove_observer(IF_Observer_Config observer); public void notify_observer(); }
IF_Observer_Config.java:
/** * Interface: Configuration class, Observer mode: Observer * New creation time: 2014/12/8 by jdh */ package ; public interface IF_Observer_Config { public void update(Config_Info info); }
:
/** * Configuration information class * New date: 2014/12/8 by jdh * Modification date: 2014/12/9 by jdh */ package ; import ; import ; import ; import ; import ; import .Inet6Address; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import org.; import org.; import org.; import .; import .; public class Config implements IF_Config,IF_Subject_Config { //Configuration information private Config_Info Info = new Config_Info(); //Storage the observer's list private List<IF_Observer_Config> Observers = new ArrayList<IF_Observer_Config>(); //Timer private Timer Timer_Work = new Timer(); //Work interval, unit: ms private final int INTERVAL_WORK = 5000; /** * Constructor */ public Config() { //Generate configuration information generate_config_info(); //Create a timed thread Timer_Work.schedule(new Task(),INTERVAL_WORK,INTERVAL_WORK); // Timing tasks } //Interface: Read and write @Override public Config_Info get_config_info() { return Info; } //Read and write, observer mode: target @Override public void register_observer(IF_Observer_Config observer) { (observer); } @Override public void remove_observer(IF_Observer_Config observer) { int index = (observer); if (index >= 0) { (observer); } } @Override public void notify_observer() { for (int i = 0; i < (); i++) { IF_Observer_Config o = (IF_Observer_Config) (i); (Info); } } /** * Get the IP address of the machine * @return native IP address */ private String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); ();) { NetworkInterface intf = (); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); ();) { InetAddress inetAddress = (); //if (!()) { if (!() && !(inetAddress instanceof Inet6Address)) { return ().toString(); } } } } catch (SocketException ex) { ("WifiPreference IpAddress", ()); } return null; } /** * Generate a String data stream of xml configuration file * Config_Info's native ip information will not be saved * @param info:Configuration information * @return xml String data stream */ private String produce_xml_string(Config_Info info) { StringWriter stringWriter = new StringWriter(); try { // Get the XmlSerializer object XmlPullParserFactory factory = (); XmlSerializer xmlSerializer = (); // Set the output stream object (stringWriter); //Start tag ("utf-8", true); (null, "config"); //title (null, "title"); (); (null, "title"); //Native port (null, "local_port"); ((info.local_port)); (null, "local_port"); //Dispatch server ip (null, "schedule_service_ip"); (info.schedule_server_ip); (null, "schedule_service_ip"); //Scheduling server port (null, "schedule_service_port"); ((info.schedule_server_port)); (null, "schedule_service_port"); (null, "config"); (); } catch (Exception e) { (); } return (); } /** * Work tasks: Get configuration information */ private void generate_config_info() { boolean ok; File sd_path; File file_cfg_dir; File file_cfg; FileOutputStream out; String str; FileInputStream in; Config_Info info = new Config_Info(); //Get the IP address of the machine info.local_ip = getLocalIpAddress(); //Get the SD card directory sd_path = (); //Judge whether the folder exists file_cfg_dir = new File(sd_path.getPath() + "//Remote_Meeting"); if (!file_cfg_dir.exists() && !file_cfg_dir.isDirectory()) { ("The configuration folder Remote_Meeting does not exist!"); ok = file_cfg_dir.mkdirs(); if (ok) { ("Folder creation succeeded!"); } else { ("Folder creation failed!"); } } //Judge whether the configuration file exists file_cfg = new File(file_cfg_dir.getPath(),""); if (!file_cfg.exists()) { ("The configuration file does not exist!"); try { file_cfg.createNewFile(); ("The file was created successfully!"); // Generate initialized configuration data try { out = new FileOutputStream(file_cfg); //Save the default configuration = "Remote Video Meeting System"; Info.local_port = 12600; Info.schedule_server_ip = "10.58.1.59"; Info.schedule_server_port = 12601; str = produce_xml_string(Info); (()); (); //Save the IP of this machine Info.local_ip = info.local_ip; //Notify observers notify_observer(); } catch (IOException e) { // TODO Auto-generated catch block (); } } catch (IOException e) { // TODO Auto-generated catch block (); } } else { //Parse xml file try { in = new FileInputStream(file_cfg); DocumentBuilderFactory factory = (); DocumentBuilder builder = (); Document document = (in); // Get the root node Element root = (); NodeList node = (); //Get the 1st child node: title = (0).getFirstChild().getNodeValue(); //Get the second child node: native port info.local_port = ((1).getFirstChild().getNodeValue()); //Get the third child node: scheduling server ip info.schedule_server_ip = (2).getFirstChild().getNodeValue(); //Get the 4th child node: Scheduling server port info.schedule_server_port = ((3).getFirstChild().getNodeValue()); //Discern whether the configuration information is changed do { if (!()) { break; } if (!info.local_ip.equals(Info.local_ip)) { break; } if (info.local_port != Info.local_port) { break; } if (!info.schedule_server_ip.equals(Info.schedule_server_ip)) { break; } if (info.schedule_server_port != Info.schedule_server_port) { break; } //All the same return; } while (false); //Assignment = ; Info.local_ip = info.local_ip; Info.local_port = info.local_port; Info.schedule_server_ip = info.schedule_server_ip; Info.schedule_server_port = info.schedule_server_port; //Notify observers notify_observer(); } catch (Exception e) { (); } } } /** * Timer thread timing operation */ private class Task extends TimerTask { @Override public void run() { generate_config_info(); } } }
:
package ; public class TestClass implements IF_Observer_Config { public TestClass () { } @Override public void update(Config_Info info) { ("------------Update data: %s,%s,%d,%s,%d\n", ,info.local_ip,info.local_port,info.schedule_server_ip,info.schedule_server_port); } }
MainActivity:
TestClass testclass = new TestClass(); Config config = new Config(); (config.get_config_info().local_ip); config.register_observer(testclass);
I hope this article will be helpful to everyone's Android programming design.