Build app update and feedback platform with WordPress (top)

Source: Internet
Author: User
Tags throwable

Demand

In the domestic this wonderful eco-environment, for Android platform mobile application, one of the necessary features is to bring the version update function-This kind of thing can be done very well with googleplay ...

Of course it does not bother to do so, to develop a backend interface is, or as I have used before: through the RSS implementation.

The other is to provide a user feedback channel, to quickly identify problems and improve, this function GooglePlay also have, but in the country you understand.

As for the domestic application market, it is too much too messy to control.

and WordPress has not been a simple blog program, and even is not a simple cms. Originally I also want to develop a comprehensive version of the update and user feedback functionality of the back-end platform, but finally found the best with WP.

Version update

The basic principle is the same as I said before the " automatic updating of the app through RSS ", but in order to simplify the development of the client, on the server to do a RSS to JSON PHP program, now this version of the client to directly parse RSS.

In order to parse RSS in the app, the most powerful Rome library is used here-but to be honest, it's a lot of pits. For example, this Rome official document is a 1.0 or earlier version of the example, but in fact the latest version is 1.5, and this 1.5 even the name of the package changed, I took a lot of detours. In addition, WP RSS2.0 some expansion function seems to Rome can not parse, so finally I used WP atom to parse, the effect is the same, because Rome can support.

The first is to get RSS asynchronously:

public class Asyncfeedfetcher extends Asynctask<string, Integer, syndfeed> {public    interface Onfetchedlistener {public        void onfetched (Syndfeed feed);    }    Private String Murl;    Private Onfetchedlistener Mcallback;    Public asyncfeedfetcher (String URL, Onfetchedlistener callback) {        murl = URL;        Mcallback = callback;    }    @Override    protected Syndfeed doinbackground (String ... params) {        syndfeed result = null;        try {            Syndfeedinput input = new Syndfeedinput ();            result = Input.build (new XmlReader (New URL (Murl)));        } catch (Throwable e) {            e.printstacktrace ();        }        return result;    }    @Override    protected void OnPostExecute (Syndfeed feed) {        if (null! = Mcallback && null! = Feed) {            MC Allback.onfetched (feed);}}}    

A standard asynchronous network request operation, nothing to say. After execution, the obtained feed object is returned to the main thread for callback via the OnPostExecute event.

The

is then the principal object that implements the automatic update:

Public class AutoUpdate implements Asyncfeedfetcher.onfetchedlistener {public interface Onupdatedlistener {Pub    LIC void onUpdated (final string version, final string message, final string URL);    } private static final String Last_check = "Auto_update_last_check";    Private Context Mcontext;    Private String mcategory;    Private Onupdatedlistener Mcallback;    Private Asyncfeedfetcher Mfetcher;    Private String mversion;    Private Sharedpreferences Mpref;        Public AutoUpdate (context context, string URL, string category, Onupdatedlistener callback) {mcontext = Context;        Mfetcher = new Asyncfeedfetcher (URL, this);        mcategory = category;        Mcallback = callback;        GetVersion ();    Mpref = preferencemanager.getdefaultsharedpreferences (context);    } Public AutoUpdate (context context, String URL) {This (context, URL, "", null); } Public AutoUpdate (context context, string URL, string category) {This (context, URL, CATegory, NULL); } public String GetVersion () {if (Textutils.isempty (mversion)) {//Set real version Comp            Onentname comp = new ComponentName (Mcontext, GetClass ());            PackageInfo pInfo = null;            try {pInfo = Mcontext.getpackagemanager (). Getpackageinfo (Comp.getpackagename (), 0); } catch (Packagemanager.namenotfoundexception e) {//do nothing} 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*1000 < now) {Mfetcher.execute ();            Sharedpreferences.editor pref = Mpref.edit ();            Pref.putlong (Last_check, now);        Pref.commit (); }} private Syndcategory Findcategory (list<syndcategory> categories, String catname) {for (syndcateGory c:categories) {if (C.getname (). Equals (CatName)) {return C;    }} return null; } private Syndentry Findentrybycategory (list<syndentry> entries, String catname) {if (Textutils.isempty (c        Atname) {return (entries.size () > 0)? entries.get (0): null; } else {for (syndentry e:entries) {if (Null! = Findcategory (E.getcategories (), Catnam                E) {return e;        }} return null; }} private string Html2txt (String message) {Pattern p = pattern.compile ("< (?: br/?|        /p|/li) \\s*> ", pattern.case_insensitive);        Message = P.matcher (message). ReplaceAll ("\ n");        p = pattern.compile ("<[^>]*>", pattern.case_insensitive);        Message = P.matcher (message). ReplaceAll ("");    return Message.trim (); private void ShowDialog (final string title, final string message, final StRing URL) {Alertdialog.builder Builder = new Alertdialog.builder (mcontext);        Builder.settitle (title);        Builder.setmessage (message); Builder.setpositivebutton (Mcontext.getstring (Android. R.string.ok), new Alertdialog.onclicklistener () {@Override public vo                        ID OnClick (dialoginterface dialog, int which) {Dialog.dismiss (); if (!                            Textutils.isempty (URL)) {Intent Intent = new Intent (Intent.action_view);                            Intent.setdata (uri.parse (URL));                        Mcontext.startactivity (Intent);        }                    }                }); Builder.setnegativebutton (Mcontext.getstring (Android. R.string.cancel), new Alertdialog.onclicklistener () {@Override publi c void OnClick (Dialoginterface dialog, int which) {DIALOG.DIsmiss ();        }                });    Builder.show (); } @Override public void onfetched (Syndfeed feed) {try {syndentry entry = findentrybycategory (fee            D.getentries (), mcategory);            if (null = = entry) {return; The Pattern p = pattern.compile (". *\\s+v\\s+ ([0-9\\.]            *) ", pattern.case_insensitive);            Matcher m = P.matcher (Entry.gettitle ());            if (!m.find ()) {return;            } String Version = M.group (1); Syndcontent content = (null! = Entry.getcontents ())? (syndcontent)            (Entry.getcontents (). Get (0)): null; String message = (null! = content)?            Content.getvalue (): "No content.";            p = pattern.compile ("<a\\s+[^>]*href\\s*=\\s*[" \ "] ([^ ' \"]*) [' \ '][^>]*> ', pattern.case_insensitive);            m = p.matcher (message);            if (!m.find ()) {return; } String url = m.group (1);           Remove download Link p = pattern.compile ("&LT;P&GT;[^&LT;]*&LT;A\\S+[^&GT;][^&LT;]*&LT;/A&GT;[^&L t;]            *</p> ", pattern.case_insensitive);            Message = P.matcher (message). ReplaceAll ("");            Message = html2txt (message); if (!version.equals (mversion) &&!  Textutils.isempty (URL)) {if (null = = Mcallback) {ShowDialog (Entry.gettitle (), message,                URL);                } else {mcallback.onupdated (version, message, URL); }}} catch (Throwable e) {//Do nothing}}}

There are several places in this thing to explain:

1, several parameters of the constructor are: context (for Operation preferences), URL (RSS link, the recommended use of atom), category (WP in the category name, you can specify a specific category in WP content for the software update, such as empty do not judge classification), Callback (the callback event when the update is found, as described below)

2, Checknow to check the updated function, the parameter is the check interval time, in minutes. If the current time and last check time interval is less than the time specified by the parameter, no action is taken, otherwise an asynchronous network request is initiated to get RSS.

3. Onfetched is the callback event for the asynchronous network request, where the obtained feed is parsed. The first is to get the latest entry record (under the specified category), then resolve the version number contained in its title and the download link contained in the content, and convert the content to plain text format. If the version is found to be different, a callback or pop-up dialog box is executed (no callback is specified).

4, because it is for the specific format to resolve, so in the WP of this designated category to publish the update article needs to be in a certain format: first of all, the title must have a full version of V, the version number should be exactly the same as the version number of the app (otherwise it will cause repeated updates), the recommended format is: "App name ", where v case can be, left or right there are no spaces can be. Next is the content is not recommended too long, so as not to display the dialog box, do not use overly complex format, do not support the picture, must have and only one link, and this link to the title version of the same app file.

5, the default dialog box is displayed as the title is WP article title, the content is converted into text WP article content (not including download link), click Confirm Automatic Call system default download tool (usually browser) to start the download link.

How to use:

Mainactivity.oncreate    mautoupdate = new AutoUpdate (This, "Http://yoursite.com/blog/?feed=atom", "Software Update", NULL);    Mautoupdate.checknow (24*60);//Mainactivity.checkupdate () {}    Mautoupdate.checknow (1);// Aboutfragment.oncreateview        Mchecknow = (Button) View.findviewbyid (r.id.btnchecknow);        Mchecknow.setonclicklistener (New View.onclicklistener () {            @Override public            void OnClick (view view) {                Mainactivity.checkupdate ();            }        });

Description

1, yoursite.com for your own URL, "software Update" for your own classification name, 24*60 that the app at startup at the time of the version update detection interval of 24 hours.

2, about the page click the "Check Now" button interval is set to a minute, to prevent users unnecessarily short time multiple clicks.

3, the above code has a known problem is: When the update detects a new version, triggering a callback event, if the current activity is not mainactivity, the default dialog box will not be displayed, so if this is the case, you need to use the last argument of the constructor, set a callback function To handle the update event yourself. A reference processing method is: If the current mainacitivity inactive, the update content first saved, let Mainacitivity in Onresume display.

In this way, the automatic Update function is implemented, and later in other applications need the same functionality, as long as a WordPress to take care of.

The next article will show you how to integrate user feedback into WordPress.

Build app update and feedback platform with WordPress (top)

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.