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