The key code to get the sunrise and sunset time is the following service. Of course, you must register and start this service in manifest.
Package COM. android. options; import android. app. service; import Java. io. bufferedreader; import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. text. simpledateformat; import Java. util. date; import android. content. sharedpreferences; import android. media. audiomanager; import android. OS. ibinder; import android. app. canbusmanager; import Droid. content. broadcastreceiver; import android. content. contentresolver; import android. content. context; import android. content. intent; import android. provider. settings; import android. util. log; import android. widget. toast; import android. OS. bundle; import android. location. location; import android. location. locationlistener; import android. location. locationmanager; import android. location. locationutils; impor T android. location. chinalocation; import Java. util. calendar; import Java. util. timer; import Java. util. timertask; public class backlightservice extends Service {Private Static double mlatitude =-1; Private Static double mlongpolling =-1; private final static string tag = "backlightservice"; private context mcontext; public static final string action_update_backlight_mode = "android. intent. action. update. ba Cklightmode "; @ overridepublic void oncreate () {super. oncreate (); mcontext = This; log. D (TAG, "year ="); savesunsetaansunrisetime (); locationmanager = (locationmanager) getsystemservice (location_service); If (locationmanager! = NULL) {log. D (TAG, "locationmanager"); locationmanager. requestlocationupdates (locationmanager. gps_provider, 2000,500, mlocationlistener) ;}@ override @ deprecatedpublic ibinder onbind (intent arg0) {// the life cycle is the same as that of the system and does not need to be bound to a certain activityreturn NULL ;} @ overridepublic void ondestroy () {super. ondestroy (); // locationmanager. removeupdates (mlocationlistener);} private void savesunsetaansunrisetime () {calendar today = Calendar. getinstance (); int year = 2013; int month = 0; int day = 16; double mlatitude = 31.308928833333333; double mlongpolling = 121.54332716666667; log. D (TAG, "year =" + year + "month =" + month); sunrisesandsetstime sun = new sunrisesandsetstime (); If (Year> 2000 & mlatitude! =-1 & mlongpolling! =-1) {sun. setdate (year, month + 1, day); double MLAT = 31.308928833333333; double mlong = 121.54332716666667; log. D (TAG, "Start"); Sun. setlat (MLAT); Sun. setlong (mlong); Sun. init (); int [] time1 = sun. getrisetime (); int risehour = time1 [0]; int riseminute = time1 [1]; log. D (TAG, "risehour =" + risehour + "riseminute =" + riseminute); int [] time2 = sun. getsettime (); int sethour = time2 [0]; int setminu Te = time2 [1]; log. D (TAG, "sethour =" + sethour + "setminute =" + setminute); If (risehour> = 0 & risehour <= 23) settings. system. putint (mcontext. getcontentresolver (), settings. system. daytime_hour, risehour); If (riseminute> = 0 & riseminute <= 59) settings. system. putint (mcontext. getcontentresolver (), settings. system. daytime_minutes, riseminute); If (sethour> = 0 & sethour <= 23) settings. system. Putint (mcontext. getcontentresolver (), settings. system. darknight_hour, sethour); If (setminute> = 0 & setminute <= 59) settings. system. putint (mcontext. getcontentresolver (), settings. system. darknight_minutes, setminute); intent I = new intent (action_update_backlight_mode); mcontext. sendbroadcast (I) ;}} private locationlistener mlocationlistener = new locationlistener () {// called when the locatio N has changed. public void onlocationchanged (location) {// todo auto-generated method stub log. D (TAG, "location changed: Location =" + location. tostring (); If (location! = NULL) {chinalocation china_lc = locationutils. wgtochina (location. getlongpolling (), location. getlatitude (); mlong1_= china_lc.china_lng; mlatloud = china_lc.china_lat; // more log. D (TAG, "location changed: latitude =" + mlatitude + ", longpolling =" + mlongpolling); // updatestat (mlatitude, mlongpolling); savesunsetaansunrisetime ();}} // called when the provider is disabled by the user. public Void onproviderdisabled (string provider) {// todo auto-generated method stub log. D (TAG, "location provider is disabled by the user");} // called when the provider is enabled by the user. public void onproviderenabled (string provider) {// todo auto-generated method stub log. D (TAG, "location provider is disabled by the user");} // called when the provider status changes. public void onstatuschan Ged (string provider, int status, bundle extras) {// todo auto-generated method stub log. D (TAG, "location provider status changes; provider =" + provider + ", status =" + status + ", extras =" + Extras. tostring () ;}}; public class sunrisesandsetstime {Private Static final string tag = "sunrisesandsetstime"; private int days_of_month_1 [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; priva Te int days_of_month_2 [] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31 }; private Static final double H =-0.833; Private Static final double Pi = 3.1415926; private int latdegree = 0; private int latdinute = 0; private int latsecond = 0; private double uto = 180.0; private int longdegree = 0; private int longdinute = 0; private int longsecond = 0; private int year, month, date; Public void setdate (int Myear, int mmonth, int mdate) {year = myear; month = mmonth; Date = mdate; log. D (TAG, "year =" + year + "month =" + month + "date =" + date);} public void setlat (double MLAT) {log. D (TAG, "setlat"); latdegree = (INT) MLAT; double num1 = (MLAT % 1) * 60; latdinute = (INT) num1; double num2 = (num1% 1) * 60; latsecond = (INT) num2; log. D (TAG, "latdegree =" + latdegree + "latdinute =" + latdinute + "latsecond =" + Latsecond);} public void setlong (double mlong) {log. D (TAG, "setlong"); longdegree = (INT) mlong; double num1 = (mlong % 1) * 60; longdinute = (INT) num1; double num2 = (num1% 1) * 60; longsecond = (INT) num2; log. D (TAG, "longdegree =" + longdegree + "longdinute =" + longdinute + "longsecond =" + longsecond);} Boolean leap_year (INT year) {If (Year % 400 = 0) | (Year % 100! = 0) & (Year % 4 = 0) return true; elsereturn false;} int days (INT year, int month, int date) {int I, a = 0; for (I = 2000; I <year; I ++) {If (leap_year (I) A = a + 366; elsea = a + 365 ;} if (leap_year (year) {for (I = 0; I <month-1; I ++) {A = a + days_of_month_2 [I] ;}} else {for (I = 0; I <month-1; I ++) {A = a + days_of_month_1 [I] ;}} A = a + date; return ;} double t_century (INT days, double UTO) {return (double) days + uto/360)/36525;} double l_sun (double t_century) {return (280.460 + 36000.770 * t_century);} double g_sun (double t_century) {return (357.528 + 35999.050 * t_century);} double transaction tic_long1_( double l_sun, double g_sun) {return (l_sun + 1.915 * Math. sin (g_sun * PI/180) + 0.02 * Math. sin (2 * g_sun * PI/180);} double earth_tilt (double t_century) {return (23.4393-0.0130 * t_century);} double sun_deviation (double earth_tilt, double maid) {return (180/pI * Math. asin (math. sin (PI/180 * earth_tilt) * Math. sin (PI/180 * effectic_long1_));} double GHA (double Uto, double g_sun, double effectic_long1_) {return (UTO-180-1.915 * Math. sin (g_sun * PI/180)-0.02 * Math. sin (2 * g_sun * PI/180) + 2.466 * Math. sin (2 * ecliptic_long1_* PI/180)-0.053 * Math. sin (4 * effectic_long1_* PI/180);} Double E (double H, double glat, double sun_deviation) {return 180/pI * Math. ACOs (math. sin (H * PI/180)-math. sin (glat x pi/180) * Math. sin (sun_deviation * PI/180)/(math. cos (glat x pi/180) * Math. cos (sun_deviation * PI/180);} double ut_rise (double Uto, double GHA, double glong, double E) {return (UTO-(GHA + glong + E);} double ut_set (double Uto, double GHA, double glong, double E) {return (UTO-(GHA + glong-E);} double result_rise (double ut, double Uto, double glong, double glat, int year, int month, int date) {double D; If (UT> = UTO) d = Ut-Uto; elsed = uto-ut; If (d >=0.1) {uto = Ut; ut = ut_rise (UTO, Gha (UTO, g_sun (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, month, date), UTO), g_sun (t_century (days (year, month, date), UTO), glong, E (H, glat, sun_deviation (earth_tilt (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO )), g_sun (t_century (days (year, month, date), UTO); result_rise (ut, Uto, glong, glat, year, month, date );} return ut;} double result_set (double ut, double Uto, double glong, double glat, int year, int month, int date) {double D; If (UT> = UTO) D = Ut-Uto; elsed = uto-ut; If (D> = 0.1) {uto = Ut; ut = ut_set (UTO, Gha (UTO, g_sun (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO )), g_sun (t_century (days (year, month, date), UTO), glong, E (H, glat, sun_deviation (earth_tilt (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO), g_sun (t_century (days (year, month, date ), UTO); result_set (ut, Uto, glong, glat, year, month, date);} return ut;} int zone (double glong) {If (glong> = 0) Return (INT) (glong/15.0) + 1); elsereturn (INT) (glong/15.0) -1);} private double glat, glong; private double rise, set; Public void Init () {log. D (TAG, "init"); glat = latdegree + latdinute/60 + latsecond/3600; // DD. input_glong (c); // C = new int [] {118, 74,023 4}; glong = longdegree + longdinute/60 + longsecond/3600; rise = result_rise (ut_rise (UTO, GHA (UTO, g_sun (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO )), g_sun (t_century (days (year, month, date), UTO), glong, E (H, glat, sun_deviation (earth_tilt (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO), g_sun (t_century (days (year, month, date ), UTO), Uto, glong, glat, year, month, date); Set = result_set (ut_set (UTO, Gha (UTO, g_sun (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO )), g_sun (t_century (days (year, month, date), UTO), glong, E (H, glat, sun_deviation (earth_tilt (t_century (days (year, month, date), UTO), maid (l_sun (t_century (days (year, month, date), UTO), g_sun (t_century (days (year, month, date ), UTO), Uto, glong, glat, year, month, date);} public int [] getrisetime () {// double times = 0; int [] times = new int [2]; Times [0] = (INT) (rise/15 + zone (glong); Times [1] = (INT) (60*(rise/15 + zone (glong)-(INT) (rise/15 + zone (glong); Return times;} public int [] getsettime () {int [] times = new int [2]; Times [0] = (INT) (Set/15 + zone (glong); Times [1] = (INT) (60*(Set/15 + zone (glong)-(INT) (Set/15 + zone (glong); Return times ;}}}