The origins
Actually, I thought about this idea as early as ten years ago when I was still working on the desktop application, and I tried it in a DELPHI program, but Because I didn’t do desktop applications anymore, this matter was let go.
I am working on mobile applications recently, and I have begun to feel the need for such a feature. Originally this kind of thing would be handled by Google Play, but because of the wonderful domestic environment, it is not realistic to rely entirely on Google Play, so most domestic applications have realized their own automatic update function.
I don’t know how other people implement the automatic update of the application, but the basic function is nothing more than: the server provides an API, and the client periodically calls to obtain the latest version information, compare it with the current version, and if it’s updated, Prompt to download updates.
The problem is that I want to simplify the process of publishing updates, so I selected RSS as the server API. In this way, every time a new version is released, I only need to post a BLOG-as long as the necessary version information is provided in it, the client can get the version update information from the RSS output.
I only need to implement an RSS parsing function on the client, but because I have implemented a set of REST client libraries, for unified access, I have made a server side and put the RSS Converted into JSON and returned.
Server
It is a simple PHP page whose function is to use CURL to read the content of RSS, and then parse it through regular expressions (too lazy to use XML), and convert it to JSON format return.
The code is as follows:
'','version' =>'', 'desc' =>'','link' =>''); if (preg_match("/- (.*?)<\/item>/is", $content, $matches)) { $item = $matches[1]; if (preg_match("/
(.*?)<\/title>/is", $item, $matches)) {$title = $matches[1]; if (preg_match("/(.*)\s+([0-9\.]*)/", $title, $matches)) {$result['name'] = $matches[1]; $result[' version'] = $matches[2];}} if (preg_match("/ (.*?)<\/description>/is", $item, $matches)) {$desc = $matches[1 ]; $desc = str_replace("<p>", "", $desc); $desc = str_replace("</p>", "\n", $desc); if (preg_match("/( .*)<a\s+.*?href=\"([^\"]*)\"/is", $desc, $matches)) {$result['desc'] = $matches[1] ; $result['link'] = $matches[2];}}} return $result;}$entry = get_entry(); echo json_encode($entry);?>
The usage is a simple REST call: GET http://yourdomain.com/check_update.php returns a JSON object with content: name, version, desc, link. They are the application name, version number, update description and download link.
The format of posting the update BLOG is:
The title is “application name xxxx”, the content is the update description, the last line puts a tag, href points to the download link, and the link text is free ( Will not appear in JSON). The version number of the x.x.x.x format must be strictly the same as the application version number in the download link, otherwise it will cause repeated updates.
Android client
The function is to perform an asynchronous REST call, obtain the returned JSON object, and then determine the version. The size is simple), a dialog box will pop up, and if you confirm the download, open the default download tool to start the download.
The Java code is as follows:
public class UpdateChecker implements AsyncRestCallListener {public static class Latest implements Serializable {private static final long serialVersionUID = 1L; public String name ; public String version; public String desc; public String link;} private static final String LATEST = "/check_update"; private static final String LAST_CHECK = "last_check"; private Context mContext; private AsyncRestCall mUpdater; private String mTitle; private String mVersion ; private SharedPreferences mPref; public UpdateChecker(Context context, String updateURL, String title) {super(); mContext = context; mUpdater = new AsyncRestCall(null, updateURL, this, null); mTitle = title; getVersion(); mPref = PreferenceManager.getDefaultSharedPreferences(context);} public String getVersion() {if (TextUtils.isEmpty(mVersion)) {Compo nentName comp = new ComponentName(mContext, getClass()); PackageInfo pinfo = null; try {pinfo = mContext.getPackageManager().getPackageInfo(comp.getPackageName(), 0);} catch (NameNotFoundException e) {e.printStackTrace( );} mVersion = pinfo.versionName;} return mVersion;} public void checkNow(int interval) {long now = (new Date()).getTime(); long lastcheck = mPref.getLong(LAST_CHECK, 0); if ( lastcheck + interval*60*1000The method of use is very simple, call checkDaily in MainActivity, and its function is to automatically check if it is more than 24 hours since the last check Check once. Then put a check button on the About page and call checkNow inside, but it is not checked every time. The fastest check frequency is limited to one minute.
The example code is as follows:
// MainActivitymChecker = new UpdateChecker(this, UPDATE_URL, this.getString(R.string.new_version));mChecker .checkDaily();// About OnCreatemChecker = new UpdateChecker(view.getContext(), UPDATE_URL, this.getString(R.string.new_version));@Overridepublic void onClick(View v) {mChecker.checkNow();}< /pre>Basically that's it. Among them, AsyncRestCall is a set of REST client libraries that I have implemented by myself. If it is not stable yet, it will not be released. Please use your favorite implementation method to implement it.