This is my work in this year's Google Android contest. I don't even have a name, so I am sad. But I have decided to work hard on Android. This is a good thing.
Check my screenshots first.
There is also a city selection interface:
Below is the code. Except for the formula, all the others are original. That is the result of my work during the October 11 holiday.
package lineter.works.sunangle;import java.util.Calendar;public class city {private double weidu, jingdu;private double pianjiao, yangjiao;public boolean weidu_cuowu = false;public boolean jingdu_cuowu = false;public city(double weidu, double jingdu) {if (weidu <= 90.0 && weidu >= -90.0 ){this.weidu = weidu;}else{weidu_cuowu=true;}if(jingdu <= 180.0&& jingdu > -180.0) {this.jingdu = jingdu;}else{jingdu_cuowu=true;}if(!weidu_cuowu && !jingdu_cuowu){this.jisuan();} else {this.pianjiao = 0.0;this.yangjiao = 0.0;}}public String getpianjiao() {String temp1, temp2, temp3;temp1 = String.valueOf((int) (this.pianjiao));temp2 = String.valueOf(Math.abs((int) ((this.pianjiao - (double) (int) (this.pianjiao)) * 60)));if (Integer.parseInt(temp2) < 10)temp2 = "0" + temp2;temp3 = String.valueOf(Math.abs((int) (((this.pianjiao - (double) (int) (this.pianjiao)) * 60 - (double) (int) ((this.pianjiao - (double) (int) (this.pianjiao)) * 60)) * 60)));if (Integer.parseInt(temp3) < 10)temp3 = "0" + temp3;return String.valueOf(temp1 + "°" + temp2 + "'" + temp3 + "\"");}public String getyangjiao() {String temp1, temp2, temp3;temp1 = String.valueOf((int) (this.yangjiao));temp2 = String.valueOf(Math.abs((int) ((this.yangjiao - (double) (int) (this.yangjiao)) * 60)));if (Integer.parseInt(temp2) < 10)temp2 = "0" + temp2;temp3 = String.valueOf(Math.abs((int) (((this.yangjiao - (double) (int) (this.yangjiao)) * 60 - (double) (int) ((this.yangjiao - (double) (int) (this.yangjiao)) * 60)) * 60)));if (Integer.parseInt(temp3) < 10)temp3 = "0" + temp3;return String.valueOf(temp1 + "°" + temp2 + "'" + temp3 + "\"");}private void jisuan() {double temp1, temp2, temp3, temp4, temp5, temp6;double chiwei, shicha;Calendar d = Calendar.getInstance();temp1 = (double) d.get(Calendar.DAY_OF_YEAR);temp2 = 79.6764 + 0.2422 * ((double) d.get(Calendar.YEAR) - 1985)- (double) ((d.get(Calendar.YEAR) - 1985) / 4);temp3 = 2 * Math.PI * (temp1 - temp2) / (365.2422);chiwei = 0.3723 + 23.2567 * Math.sin(temp3) + 0.1149* Math.sin(2 * temp3) - 0.1712 * Math.sin(3 * temp3) - 0.758* Math.cos(temp3) + 0.3656 * Math.cos(2 * temp3) + 0.0201* Math.cos(3 * temp3);shicha = 0.0028 - 1.9857 * Math.sin(temp3) + 9.9059* Math.sin(2 * temp3) - 7.0924 * Math.cos(temp3) - 0.6882* Math.cos(2 * temp3);temp4 = (double) d.get(Calendar.HOUR_OF_DAY)+ ((double) d.get(Calendar.MINUTE)) / 60+ ((double) d.get(Calendar.SECOND)) / 3600;temp4 = temp4 + (this.jingdu - 120.0) * 4 / 60 + shicha / 60;temp5 = Math.sin(this.weidu * Math.PI / 180)* Math.sin(chiwei * Math.PI / 180)+ Math.cos(this.weidu * Math.PI / 180)* Math.cos(chiwei * Math.PI / 180)* Math.cos((temp4 - 12) * 15 * Math.PI / 180);yangjiao = Math.asin(temp5) * 180 / Math.PI;temp6 = (Math.sin(yangjiao * Math.PI / 180)* Math.sin(this.weidu * Math.PI / 180) - Math.sin(chiwei* Math.PI / 180))/ (Math.cos(yangjiao * Math.PI / 180) * Math.cos(this.weidu* Math.PI / 180));pianjiao = Math.acos(temp6) * 180 / Math.PI;if (temp4 < 12.0) {pianjiao = -pianjiao;}}}
This is the main program,
Sunangle. Java:
Package lineter. works. sunangle; import Java. util. arraylist; import Java. util. list; import android. app. activity; import android. app. alertdialog; import android. content. sharedpreferences; import android. OS. bundle; import android. OS. handler; import android. view. view; import android. widget. adapterview; import android. widget. arrayadapter; import android. widget. button; import android. widget. edittext; import android. W Idget. spinner; import android. widget. textview; public class sunangle extends activity {public static final string all_date = "position"; private edittext etweidu, etjingdu; private button bcalc, bhelp, bquit; private textview tvpianjiao, tvyangjiao; final handler = new handler (); Private list <string> List = new arraylist <string> (); Private arrayadapter <string> adapter; private spinner sp; private Bo Olean firstchoice = true; private int citychoic; private string [] city_name = {"Beijing", "Tianjin", "Shijiazhuang", "Baoding", "Tangshan", "Qinhuangdao ", "Zhangjiakou", "Chengde", "Taiyuan", "Datong", "Linfen", "Changzhi", // 12 "Hohhot", "Baotou", "Hailar ", "Shenyang", "Dalian", "Anshan", "Jinzhou", "Changchun", "Jilin", "Harbin", "Qiqihar", "Mudanjiang", "Shanghai ", // 13 "Nanjing", "Wuxi", "Suzhou", "Xuzhou", "Hangzhou", "Ningbo", "WenZhou", "Jinhua", "Hefei ", "Wuhu", "Anqing", "Fuzhou", "Xiamen", "Quanzhou", "Nanchang", // 15 "Jiujiang", "Ganzhou", "Jinan ", "Yantai", "Qingdao", "Zhengzhou", "Kaifeng", "Luoyang", "xuchang", "Xinxiang", "Wuhan", "Yichang", "Sha City ", "Changsha", "Hengyang", // 15 "Xiangtan", "Changde", "Guangzhou", "Shantou ", "Shaoguan", "Haikou", "Nanning", "Guilin", "Liuzhou", "Wuzhou", "Chengdu", "Chongqing", "Neijiang", "Luzhou ", "Wan county", // 15 "Guiyang", "Zunyi", "Kunming", "Lhasa", "Shigatse", "Xi'an", "Baoji", "yan'an ", "Lanzhou", "Tianshui", "Jiuquan", "Xining", "Yinchuan", "Urumqi", // 14 "Hami", "Kashgar", "Hotan ", "Taipei"}; // 4 private double [] city_longpolling = {116.467, 117.167, 114.600, 115.467, 118.150, 119.617, 114.917, 117.867, 112.550, 113.217, 111.217, 113.217, // 12 111.633, 110.000, 119.717, 123.383, 121.633, 123.000, 121.150, 125.300, 126.600, 126.633, 123.917, 129.600, 121.433, // 13 118.767, 120.300, 120.650, 117.200, 120.167, 121.567, 120.633, 119.817, 117.267, 118.333, 117.033, 119.317, 118.067, 118.617, 15, 115.883, 114.933, 117.033, 121.333, 120.317, 113.700, 114.383, 112.433, 113.800, 113.900, 114.333, 111.250, 112.283, 112.917, 112.567, 112.850, 111.650, 113.300, 116.667, 113.550, 110.167, 108.350, 110.167, 109.317, 111.300, 104.067, 106.550, 105.050, 105.450, 108.367, 106.717, // 15 106.883, 102. 700, 91.033, 88.817, 108.917, 107.150, 109.433, 103.833, 105.550, 98.500, 101.817, 106.216, 87.600, 93.450, 75.983, 79.917 }; // 4 private double [] city_dimensionality = {39.900, 39.167, 38.050, 38.883, 39.617, 39.900, 40.850, 40.983, 37.850, 40.117, 36.083, 36.083, 40.583, 49.233, 41.800, 38.900, 41.067, 41.150, 43.917, 43.800, 45.750, 47.367, 44.583, 31.200, 32.050, // 13 31.583, 31.333, 34.267, 30.250, 29.883, 28.000, 29.167, 31.850, 31.350, 30.533, 26.033, 24.433, 24.900, 28.683, // 15 29.717, 25.850, 36.667, 37.550, 36.067, 34.733, 34.867, 34.717, 34.000, 35.300, 30.617, 30.700, 30.267, 28.200, 26.917, // 15 27.900, 29.000, 23.167, 23.350, 24.800, 20.050, 22.783, 25.300, 24.333, 23.467, 30.650, 29.550, 29.583, 28.900, 30.800, // 15 26.567, 27.750, 25.050, 29.650, 29.267, 34.250, 34.350, 36.583, 36.050, 34.583, 39.733, 36.617, 38.467, 43.767, 42.833, // 14, 39.450, 37. 117, 25.033}; // 4/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); settitle ("sun angle computing tool"); setcontentview (R. layout. main); etweidu = (edittext) findviewbyid (R. id. weidu); etjingdu = (edittext) findviewbyid (R. id. jingdu); bcalc = (button) findviewbyid (R. id. bcalc); bcalc. setonclicklistener (New clicklistener (); bhelp = (Button) findviewbyid (R. id. bhelp); bhelp. setonclicklistener (New clicklistener (); bquit = (button) findviewbyid (R. id. bquit); bquit. setonclicklistener (New clicklistener (); tvpianjiao = (textview) findviewbyid (R. id. pianjiao); tvyangjiao = (textview) findviewbyid (R. id. yangjiao); handler. postdelayed (runnable, 1000); int TMP; For (TMP = 0; TMP <city_name.length; TMP ++) {list. add (city_name [TMP]. tostring ();} sp = (SP Inner) findviewbyid (R. layout. myspinner); adapter = new arrayadapter <string> (this, android. r. layout. simple_spinner_item, list); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); SP. setadapter (adapter); SP. setonitemselectedlistener (new Spinner. onitemselectedlistener () {@ overridepublic void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubif (firstchoice) {firstchoice = false;} else {etjingdu. settext (string. valueof (city_long1_[ arg2]); etweidu. settext (string. valueof (city_dimensionality [arg2]); citychoic = arg2;} arg0.setvisibility (view. visible) ;}@ overridepublic void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub}); sharedpreferences position1 = getsharedpreferences (all_date, 0); string lastweidu = position1.getstring ("last_weidu", "38.497 "); string lastjingdu = position1.getstring ("last_jingdu", "106.133"); citychoic = position1.getint ("citychoic", 0); etweidu. settext (lastweidu. tostring (); etjingdu. settext (lastjingdu. tostring (); SP. setselection (citychoic);} @ overrideprotected v Oid onstop () {// todo auto-generated method stubsuper. onstop (); sharedpreferences. editor upload data = getsharedpreferences (all_date, 0 ). edit (); upload data. putstring ("last_weidu", etweidu. gettext (). tostring (); returns data. putstring ("last_jingdu", etjingdu. gettext (). tostring (); returns data. putint ("citychoic", citychoic); invalid data. commit () ;}@ overrideprotected void ondestroy () {// todo auto-generated Method Stubsuper. ondestroy (); handler. removecallbacks (runnable);} class clicklistener implements view. onclicklistener {@ overridepublic void onclick (view arg0) {// todo auto-generated method stubif (arg0 = bcalc) {city Ct = New City (double. parsedouble (etweidu. gettext (). tostring (), double. parsedouble (etjingdu. gettext (). tostring (); tvpianjiao. settext (Ct. getpianjiao (); tvyangjiao. settext (Ct. getyangjiao ());} Else if (arg0 = bhelp) {New alertdialog. builder (sunangle. this ). settitle ("help "). setmessage ("select a city or enter the local longitude and latitude to calculate the current time sun angle. \ N "+" Description: \ n "angle" is 0 degrees in the positive and south directions, and the East and West are positive. \ N "+" "elevation of angle" is 0 degrees on the ground plane and is positive and negative. ". Tostring ()). show () ;}else if (arg0 = bquit) {finish () ;}} runnable = new runnable () {@ overridepublic void run () {// todo auto-generated method stubtry {double tmp1, tmp2; If (etweidu. gettext (). tostring ()! = "" & Etjingdu. gettext (). tostring ()! = "") {Tmp1 = double. parsedouble (etweidu. gettext (). tostring (); tmp2 = double. parsedouble (etjingdu. gettext (). tostring (); City Ct = New City (tmp1, tmp2); tvpianjiao. settext (Ct. getpianjiao (); tvyangjiao. settext (Ct. getyangjiao (); If (Ct. weidu_cuowu) {tvpianjiao. settext ("dimension input error");} If (Ct. jingdu_cuowu) {tvyangjiao. settext ("longitude input error");} handler. postdelayed (this, 1000) ;}} catch (exception e) {handler. postdelayed (runnable, 1000 );}}};}
Look at the code and you will know how I have paid for it.
Sorry, I have to upload fewer layout files.
Main. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: background = "@ drawable/Taiyang"> <tablelayout Android: layout_width = "fill_parent" Android: stretchcolumns = "0, 1" Android: layout_height = "fill_parent" Android: padding = "10dp"> <tablerow> <textviewandroid: text = "select city" Android: textsize = "18dp"/> <spinner Android: id = "@ + layout/myspinner" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"/> </tablerow> <textviewandroid: TEXT = "local dimension (°)" Android: textsize = "18dp"/> <edittext Android: Id = "@ + ID/weidu" Android: text = "38.16" Android: numeric = "decimal"/> </tablerow> <textview Android: text = "Local longitude (°)" Android: textsize = "18dp"/> <edittext Android: id = "@ + ID/jingdu" Android: text = "106.1" Android: Numeric = "decimal"/> </tablerow> <textviewandroid: TEXT = "" Android: textsize = "18dp"/> <textviewandroid: Id = "@ + ID/pianjiao" Android: text = "0.0" Android: textsize = "18dp" Android: textcolor = "#4754f8"/> </tablerow> <textviewandroid: text = "" Android: textsize = "18dp"/> <textviewandroid: Id = "@ + ID/yangjiao" Android: text = "0.0" Android: textsize = "18dp" Android: textcolor = "#4754f8"/> </tablerow> <textviewandroid: text = "Current Time" Android: textsize = "22dp"/> <digitalclock Android: TEXT = "digitalclock" Android: Id = "@ + ID/digitalclock1" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: textsize = "22dp" Android: textcolor = "# 0ff"/> </tablerow> <buttonandroid: Id = "@ + ID/bhelp" Android: text = "help" Android: layout_width = "wrap_content"/> <buttonandroid: Id = "@ + ID/bquit" Android: text = "Exit System" Android: layout_width = "wrap_content"/> </tablerow> <button Android: Id = "@ + ID/bcalc" Android: text = "computing angle" Android: layout_width = "wrap_content" Android: visibility = "invisible"/> </tablelayout> </linearlayout>
String File
String. xml:
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, sunangle! </String> <string name = "app_name"> sunangle </string> <string name = "helpmsg"> help. </String> </resources>
Androidmanifest. xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="lineter.works.sunangle" android:versionCode="1"android:versionName="1.0"><uses-sdk android:minSdkVersion="4" /><application android:icon="@drawable/sunangle"android:label="@string/app_name" android:launchMode="singleInstance"><activity android:name=".sunangle" android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
In fact, there is no permission, and it does not need any permissions, but there is a singleton running setting. In the eighth row, pay attention to it.