Android modifies the system time zone

Source: Internet
Author: User
Tags time zones

dynamic registered broadcast receivers must have an instance present Set the system time zone:
Alarmmanager Malarmmanager = (alarmmanager) getsystemservice (context.alarm_service);Malarmmanager.settimezone ("gmt+08:00");
Setting the time zone requires adding the following permissions in Manifest.xml:<!--allows setting time zones --<uses-permissionandroid: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 ();//Can be modified individually for each time domain

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", "background process is created ... ");
Intentfilter intentfilter = new Intentfilter ("Closeservice");
ZoneService.this.registerReceiver (Stopservicereceiver, Intentfilter);
The service initiates the broadcast receiver, allowing the broadcast receiver to continue executing after the program exits, receiving system time to change the broadcast event

ZoneService.this.registerReceiver (timerchangered, New Intentfilter (
Intent.action_time_tick));

}

@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {

LOG.I ("Liujun", "background process ... ");
Return Super.onstartcommand (Intent, flags, Startid);

}

@Override
public void OnDestroy () {

LOG.I ("Liujun", "background process is destroyed ... ");
Super.ondestroy ();
}

@Override
Public IBinder Onbind (Intent Intent) {
TODO auto-generated method stubs
return null;
}

}

MainActivity.this.sendBroadcast (New Intent ("Closeservice"));MainActivity.this.startService (New Intent (Mainactivity.this,zoneservice.class));

Android modifies the system time zone

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.