Java enumeration in Android project app

Source: Internet
Author: User

Today fix a company's long-ago Android app feature, the code logic is completely confused, and then found that the returned data is completely wrong. And then fix it for a full two days. Then I tidied it up again, and the refactoring is not even up. The enumeration is then used.

What is an enumeration? I didn't know that at the time, I saw the company project using enumerations as a control of the project, such as modifying an already written app and then making a custom version for some handset makers. You may want to get rid of ads, and jump to the store URL are different, especially in the domestic basic no Google Play. In order to avoid future changes, we will write an enumeration to control it.

Public enum Market {Default,huawei () {@Overridepublic String Getmarketurl () {return "http://play.huawei.com";//huawei Market Url}},zte () {@Overridepublic Boolean isshouldad () {return false;} @Overridepublic String Getmarketurl () {return "http://play.zte.com";//zte market Url}},onetouch () {@Overridepublic String Getmarketurl () {return "http://play.huawei.com";}}; public Boolean Isshouldad () {return true;} Public String Getmarketurl () {return "Http:\\googleplay ...";//google play URL}}

The above example will probably understand some of the basic uses of Java enumeration in Android. To understand the principles of Java enumeration, I wrote a very common example of traffic lights. Here is the code with the enumeration:

public enum TrafficLight {red {@Overridepublic trafficlight Nextlamp () {return Green;}},green () {@Overridepublic TrafficLight Nextlamp () {return Yellow;}},yellow (3) {@Overridepublic TrafficLight nextlamp () {return red;}}; private int time;private trafficlight (int time) {this.time = time;}; Public abstract TrafficLight Nextlamp ();p ublic int getTime () {return this.time;}}
then the code for the normal class simulation enum:

Public abstract class TrafficLight {public static final trafficlight red  = new TrafficLight ($) {@Overridepublic Traff Iclight Nextlamp () {return green;}; public static final TrafficLight green  = new TrafficLight (+) {@Overridepublic TrafficLight nextlamp () {return yellow ;}}; public static final TrafficLight yellow  = new TrafficLight (3) {@Overridepublic TrafficLight nextlamp () {return red;}} ;p rivate int time;private trafficlight (int time) {this.time = time;}; Public abstract TrafficLight Nextlamp ();p ublic int getTime () {return this.time;}}
with two comparisons, you will find that the enumeration is an ordinary Java class, only a private construction method, and then provides several static final instance variables. Of course, Enum also provides some other methods. For example: TrafficLight.green.name () is still very useful.

These are some of the basic applications of enum. And then I am today in the project how to apply the type with enumerations. Because our app has three different URL for requesting data. In fact, we only have one data source, if find does not, it will be through the other two is to read other Web site HTML, and then parse, through regular expression matching to get the data. Each data source needs to set parameters such as HttpClient, HttpGet, HttpResponse, and then use the enumeration. I put a little bit of basic in here. And then it turns out that it's all the same.

Import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;public enum Requestedprovider {myself () {@Overridepublic String getUrl (String KeyWord) {return "http://..." + KeyWord + "...";} ...},google () {@Overridepublic string GetUrl (String keyWord) {return "Http://google ..." + KeyWord + "...";} ...},amazon () {@Overridepublic string GetUrl (String keyWord) {return "Http://amazon ..." + KeyWord + "...";} ...}; Public abstract String GetUrl (String keyWord);p ublic HttpClient pickhttpclient () {return new defaulthttpclient ();} Public httpget pickhttpget (String URL) {return new HttpGet (URL);} Public HttpResponse Pickhttpresponse (HttpClient client, HttpGet get) {HttpResponse res = null;try {res = Client.execute (ge t);} catch (Clientprotocolexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return res;} ...}
That's what I'm going to write about today. ye!




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.