android獲得日升日落時間

來源:互聯網
上載者:User

獲得日升日落時間的關鍵代碼就是下面這個服務,當然要去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 android.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;import 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 mLongitude = -1;private final static String TAG = "BacklightService";private Context mContext;    public static final String ACTION_UPDATE_BACKLIGHT_MODE = "android.intent.action.update.backlightmode";@Overridepublic void onCreate() {super.onCreate();mContext = this;Log.d(TAG, "year=");saveSunSetAanSunRiseTime(); LocationManager 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) {// 生命期與系統一致,不需要綁定到某個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 mLongitude = 121.54332716666667;Log.d(TAG, "year=" + year + "  month=" + month);        SunRisesAndSetsTime sun = new SunRisesAndSetsTime();                if (year > 2000 && mLatitude != -1 && mLongitude != -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 setMinute = 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 location has changed.        public void onLocationChanged(Location location) {            // TODO Auto-generated method stub            Log.d(TAG, "Location Changed : location = " + location.toString());            if (location != null) {                ChinaLocation china_lc = LocationUtils.wgtochina(location.getLongitude(), location.getLatitude());                mLongitude = china_lc.china_lng;                mLatitude = china_lc.china_lat;                // more                Log.d(TAG, "Location Changed : Latitude = " + mLatitude + ", Longitude = "                        + mLongitude);//                updateStat(mLatitude, mLongitude);                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 onStatusChanged(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 };private int days_of_month_2[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 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 a;}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 ecliptic_longitude(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 ecliptic_longitude) {return (180 / PI * Math.asin(Math.sin(PI / 180 * earth_tilt) * Math.sin(PI / 180 * ecliptic_longitude)));}double GHA(double UTo, double G_sun, double ecliptic_longitude) {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_longitude * PI / 180) - 0.053 * Math.sin(4 * ecliptic_longitude * PI / 180));}double e(double h, double glat, double sun_deviation) {return 180/ PI* Math.acos((Math.sin(h * PI / 180) - Math.sin(glat * PI / 180) * Math.sin(sun_deviation * PI / 180))/ (Math.cos(glat * 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)),ecliptic_longitude(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)),ecliptic_longitude(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)),ecliptic_longitude(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)),ecliptic_longitude(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) ((int) (glong / 15.0) + 1);elsereturn (int) ((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, 0234 };glong = longDegree + longDinute / 60 + longSecond / 3600;rise = result_rise(UT_rise(UTo,GHA(UTo,G_sun(t_century(days(year, month, date), UTo)),ecliptic_longitude(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)),ecliptic_longitude(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)),ecliptic_longitude(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)),ecliptic_longitude(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;}  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.