Combination of sharedpreferences and properties in Android

Source: Internet
Author: User

Recently, the company's entire product client has basically entered stable commercial operation.

Later, we need to deal with version control with our partners.

For example, the entire model of A, B, and C partners remains unchanged in terms of applications. It may only change the next several transmission parameters or the setting parameters of the application;

Of course, this is relatively simple. It may be said that you modify the version number of a dead merchant before packaging each application.

Later, we proposed a tough requirement that the display sequence on the homepage of our application or in a certain interface should be able to be controlled.

In theory, the list display control is relatively simple, that is, all my information is obtained from the server, and the order is controlled,

It can be completed on the server.

However, this is another issue. It is also a requirement that, if there is no network, no information is returned and the interface is blank?

Solution: 1. You can consider caching. If there is no network, I will read the last saved content. However, our product homepage information is for real-time operations, rather than

News applications have no impact on user operations.

2. Set a set of default interface configurations. That is, if there is no network, at least we can display the specified image and text of the prompt when no content is filled.

Then we can write this configuration information in a configuration file and write preference during application installation (for the reason why the write preference is required, of course, to consider the next parameter read rate of the application .)

The following is a simple idea based on our application needs.

Step 1: Create a properties file under the assets file and write the configuration version number (because our application involves the list display Sorting Problem, This sorting is for the client,

After the application is packed, it is written to the dead, and the sorting needs to be changed in the future. For example, if the hot list changes up or down, I can only control it through the server .),

This version number is used to obtain the latest configuration file from the server when the application is started (that is, the server also has such a version number. if the version number is the same, a flag value is returned, returns the latest configuration .)

Step 2: When the app is started, write the properties file to sharedpreferences after judgment. At this moment, another problem will be taken into account: I cannot write the file every time the app is started,

This operation is unreasonable, that is, we need a flag to determine whether the configuration file has been written (that is, the configuration file only needs to be written once during application installation ).

Set a configstatus to judge.

At this time, there will be another problem. If my application is upgraded, because as far as I know, after your application is upgraded and overwritten, the preference files appear to be all in, that is, it is unclear to uninstall them,

The configuration file included in the new version will not be written because it determines configstatus.

Therefore, we are considering a tag bit. Here we use the application version number. Every time the version number is changed, you can determine that the properties file needs to be written again after the upgrade.

Private configurationutils () {sp = mcontext. getsharedpreferences (constant. configuration_file_name, constant. configuration_file_mode); If (! Exsitsharedpreferences () {logutil. log ("configurationutils", "sharedpreferences not configured, start to configure"); defaultproperty = new properties (); file2sharedpreferences (defaultproperty, SP );}}; static class holder {static configurationutils instance = new configurationutils ();} public static configurationutils getinstance () {log. I ("configurationutils", "Run -- configurationutils. holder. instance "); Return configurationutils. ho Lder. instance;}/***** @ return */public static Boolean exsitsharedpreferences () {string currentvername = "null"; try {currentvername = mcontext. getpackagemanager (). getpackageinfo (constant. packagename, 0 ). versionname;} catch (namenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} return sp. getstring ("configuratonstatus", "0 "). equals ("0") |! Sp. getstring ("version", "0"). Equals (currentvername )? False: true;}/*** write the propties file configuration in assets to sharedpreferences * @ return */public static Boolean file2sharedpreferences (properties prop, sharedpreferences preferences) {try {sp. edit (). clear (). commit (); // load the configuration file prop. load (mcontext. getassets (). open ("jifenka. properties "); iterator it = prop. entryset (). iterator (); While (it. hasnext () {map. entry entry = (map. entry) it. next (); string key = (string) entry. getkey (); string value = (string) entry. getvalue (); preferences. edit (). putstring (Key, value ). commit ();} preferences. edit (). putstring ("configuratonstatus", "1 "). commit ();} catch (ioexception e) {// todo auto-generated catch blockreturn false;} return false ;}

The above code is written into the judgment code.

Step 3: After the application enters the homepage, the order of the displayed list can be obtained from preference.

/*** Obtain the corresponding configuration file stringvalue * @ Param context * @ Param key */public static string getstringvaluebyspecifiedkeyinconfigsp (context, string key) {string value based on the specified key; sharedpreferencessp = context. getsharedpreferences (constant. configuration_file_name, constant. configuration_file_mode); value = sp. getstring (key, "0"); return value ;}

This is the basic process.

If the version number is different, update the preference.

/*** Update or add configuration information. * @ Param context * @ Param infomap */public static void setconfiginfosp (context, Map <string, string> infomap) {sharedpreferences sp = context. getsharedpreferences (constant. login_info, constant. configuration_file_mode); set <string> set = infomap. keyset (); For (iterator it = set. iterator (); it. hasnext ();) {string S = (string) it. next (); SP. edit (). putstring (S, infomap. get (s )). commit ();}}

In this way, the server can control the list in the order that the default list can be displayed without a network.

The introduction of configuration files can be used to control multiple versions of the same application in my company.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.