Java enumeration for android Projects

Source: Internet
Author: User

Today, I fixed a company's android app feature that was a long time ago. The Code logic in the app is completely disordered, and I found that the returned data is completely incorrect. Then it was fixed for two days. Then I reorganized it again, and the refactoring won't be enough. Then the enumeration is used.

What is enumeration? I didn't understand it before. At that time, I saw that the company's project used enumeration as a project control, such as modifying the already written app and then making customized versions for some mobile phone manufacturers. The advertisement may need to be removed, and the url to jump to the store may be different. In particular, there is basically no google play in China. To avoid future modification, 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 shows the basic usage of java enumeration in android. To understand the principles of java annotations, I wrote a common traffic light example. The following code uses enumeration:

public enum TrafficLight {red(45) {@Overridepublic TrafficLight nextLamp() {return green;}},green(30) {@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();public int getTime() {return this.time;}}
Then, the code for simulating enum in a common class is as follows:

public abstract class TrafficLight {public static final TrafficLight red  = new TrafficLight(45){@Overridepublic TrafficLight nextLamp() {return green;}};public static final TrafficLight green  = new TrafficLight(30) {@Overridepublic TrafficLight nextLamp() {return yellow;}};public static final TrafficLight yellow  = new TrafficLight(3) {@Overridepublic TrafficLight nextLamp() {return red;}};private int time;private TrafficLight(int time) {this.time = time;};public abstract TrafficLight nextLamp();public int getTime() {return this.time;}}
Through two comparisons, we will find that enumeration is actually a common java class, only a private constructor, and then several static final instance variables are provided. Of course, enum also provides some other methods. For example, TrafficLight. green. name () is very useful.

These are some basic applications of enum. Then how can I apply the enumeration type in my project today. Because our app has three different request data URLs. In fact, we only have one data source. If we cannot find the data, we will read the html of other websites through the other two, parse the data, and get the data through regular expression matching. For each data source, you must set parameters such as httpClient, httpGet, and httpResponse, and then use enumeration. I will post some basic information here. Then I found that they are all similar.

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://google..." + keyWord + "...";}...};public abstract String getUrl(String keyWord);public 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(get);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return res;}...}
This is what we wrote 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.