我有一个奇怪的问题。在我的Android应用程序中,我有10毫秒计时器运行。 经过长时间运行(运行14小时)后,计时器如何导致设备时间的变化。
通常情况会提前2天改变
public class MainActivity extends Activity {
TextView DateTextView;
TextView BatteryTextView;
String date;
Thread mUpdateUIThread;
public native void WatchDogTimeTest();
int[] m_array ;
Timer mTimerThread;
String StrDdate;
int n = 0;
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();
WatchDogTimeTest();
}
public void CreateMutipleThread()
{
for(int i = 0 ; i < 100 ; i++)
{
mUpdateUIThread= new Thread() {
@Override
public void run() {
try {
while (!isInterrupted())
{
Thread.sleep(100); //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());
}
},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);
}
}
};
}
知道如何解决这个问题。这是我的测试代码