我有一个基于Android ICS的设备。没有任何应用程序运行它将不会改变时间。但每当我运行一个应用程序时,它会随机更改时间(大多数提前2天)。
没有设定时间功能用于设置设备时间。这个程序是我写的。它在每个活动上有许多线程和1毫秒计时器。
设备在运行12小时后更改了时间。
为什么设备时间在变化?此计时器或线程是否会导致问题,从而改变设备时间。
public class MainActivity extends Activity {
TextView DateTextView;
TextView BatteryTextView;
String date;
Thread mUpdateUIThread;
int[] m_array ;
Timer mTimerThread;
String StrDdate;
int n = 0;
public native void WatchDogTimeTest();
public IntentFilter s_intentFilter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DateTextView = (TextView)findViewById(R.id.DatedisplayTxtVw);
BatteryTextView = (TextView)findViewById(R.id.BatteryStatus);
date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
DateTextView.setText(date);
s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_DATE_CHANGED);
s_intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
s_intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
s_intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
this.registerReceiver(this.mBatInfoReceiver,s_intentFilter);
CreateMutipleThread();
TimerThread();
}
public void CreateMutipleThread()
{
for(int i = 0 ; i < 100 ; i++)
{
mUpdateUIThread= new Thread() {
@Override
public void run() {
try {
while (!isInterrupted())
{
Thread.sleep(1000); //Check for Aux Battery Status After 10 Second
runOnUiThread(new Runnable()
{
@Override
public void run()
{
n++;
if(n >= 10000)
n= 0;
}
});
}
} catch (InterruptedException e) {
}
}
};
mUpdateUIThread.start();
}
}
public void TimerThread()
{
mTimerThread = new Timer();
//Set the schedule function and rate
mTimerThread.schedule(new TimerTask() {
@Override
public void run() {
StrDdate = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
//Log.d("TimeDevice","Time"+StrDdate);
}
},1000,1);
}
public void InvalidateTimer()
{
this.runOnUiThread(new Runnable() {
@Override
public void run() {
printTime();
}
});
}
public void printTime()
{
}
public BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
final String action = intent.getAction();
//Date Change Broad cast
if(action.equals(Intent.ACTION_DATE_CHANGED))
{
String mydate java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
date = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
DateTextView.setText(date);
DateTextView.setBackgroundColor(Color.RED);
BatteryTextView.setText(mydate);
}
}
};
}