码迷,mamicode.com
首页 > 移动开发 > 详细

android修改系统时区

时间:2015-04-25 16:24:47      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

 动态注册广播接收器必须有实例存在
 
设置系统时区:

AlarmManager mAlarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mAlarmManager.setTimeZone("GMT+08:00");

设置时区需要在manifest.xml中增加如下权限:
<!-- 允许设置时区-->
<uses-permission android:name="android.permission.SET_TIME_ZONE" />
 
package com.lzh.modeify;

import java.util.Calendar;
import java.util.Timer;

import android.R.integer;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;

public class ZoneService extends Service {
    private static final String ACTION_DATE_CHANGED = Intent.ACTION_DATE_CHANGED;
    private static final String ACTION_TIME_CHANGED = Intent.ACTION_TIME_CHANGED;

    private BroadcastReceiver TimerChangered = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {

            Calendar c = Calendar.getInstance();// 可以对每个时间域单独修改

//            int year = c.get(Calendar.YEAR);
//            int month = c.get(Calendar.MONTH);
//            int date = c.get(Calendar.DATE);
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);
            int second = c.get(Calendar.SECOND);

            Log.v("time", Integer.toString(hour)+Integer.toString(minute)+Integer.toString(second));
        }
    };

    private BroadcastReceiver stopServiceReceiver = new BroadcastReceiver() {

        public void onReceive(android.content.Context context, Intent intent) {
            ZoneService.this.stopSelf();
            ZoneService.this.unregisterReceiver(stopServiceReceiver);
        };
    };

    @Override
    public void onCreate() {

        super.onCreate();
        Log.i("liujun", "后台进程被创建。。。");
        IntentFilter intentFilter = new IntentFilter("closeService");
        ZoneService.this.registerReceiver(stopServiceReceiver, intentFilter);
        // 服务启动广播接收器,使得广播接收器可以在程序退出后在后天继续执行,接收系统时间变更广播事件

        ZoneService.this.registerReceiver(TimerChangered, new IntentFilter(
                Intent.ACTION_TIME_TICK));

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i("liujun", "后台进程。。。");
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {

        Log.i("liujun", "后台进程被销毁了。。。");
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO 自动生成的方法存根
        return null;
    }

}

 
    MainActivity.this.sendBroadcast(new Intent("closeService"));
 
    MainActivity.this.startService(new Intent(MainActivity.this,ZoneService.class));

android修改系统时区

标签:

原文地址:http://www.cnblogs.com/lzh-Linux/p/4455932.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!