当前位置:操作系统 > 安卓/Android >>

android获得日升日落时间

获得日升日落时间的关键代码就是下面这个服务,当然要去manifest里注册和启动这个服务

[html] 

[html] 

[html] 
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"; 
     
    @Override 
    public 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 
    @Deprecated 
    public IBinder onBind(Intent arg0) { 
 
        // 生命期与系统一致,不需要绑定到某个activity 
        return null; 
    } 
     
    @Override 
    public 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); 补充:移动开发 , Android ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,