我正在尝试构建一个应用程序,在该应用程序中,我尝试永久获取同一应用程序中的系统时间和ntp时间,并将其写入excel文件。虽然这可以正常工作约5-10s。异步任务会在大约5s和随机10s后失败。我仍然试图将其范围缩小到asynctask,因为我已经在服务中实现了它。请让我知道我在哪里出错,以便我可以解决它。
MainActivity.java
public class MainActivity extends AppCompatActivity {
static excel excel;
static int index = 1;
int cell = 0;
EditText server;
Intent intent;
static TextView system1;
static TextView ntp;
Button start1,stop;
static String server_name;
NtpV3Packet message;
long diff;
String time2;
Date time;
DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss_SSS");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(MainActivity.this, NtpUpdate.class);
server = (EditText)findViewById(R.id.Server12);
ntp = (TextView)findViewById(R.id.ntp_time);
system1 = (TextView)findViewById(R.id.System_Time);
start1 = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
server_name = "0.north-america.pool.ntp.org";
excel = new excel();
excel.createsheet();
excel.createrow(0);
excel.createcell(0,"NTP Time");
excel.createcell(1,"System Time");
excel.createcell(2,"Difference");
excel.write();
}
public static String getservername(){
return server_name;
}
public static void updateui(String data){
String[] parts = data.split(";;;;");
String part1 = parts[0]; // 004
String part2 = parts[1];
String part3 = parts[2];
ntp.setText(part1);
system1.setText(part2);
excel.createrow(index);
excel.createcell(0,part1);
excel.createcell(1,part2);
excel.createcell(2,part3);
excel.write();
index++;
}
public void start1(View view){
intent.putExtra("start",1);
startService(intent);
}
public void stop1(View view){
intent.putExtra("start",0);
stopService(intent);
}
}
class excel{
HSSFWorkbook workbook;
static HSSFSheet firstSheet;
static HSSFCell cellA;
static HSSFRow rowA;
File file = new
File(String.valueOf(Environment.getExternalStorageDirectory()) + File.separator + "data.xls");
public void createsheet(){
workbook = new HSSFWorkbook();
firstSheet = workbook.createSheet("NtpLog");
}
public static void createrow(int index){
rowA = firstSheet.createRow(index);
}
public static void createcell(int index, String text){
cellA = rowA.createCell(index);
cellA.setCellValue(text);
}
public void write(){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
workbook.write(fos);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
NTpUpdate.java(service)
public class NtpUpdate extends Service {
String server_name = "0.north-america.pool.ntp.org";
Timer myTimer = new Timer("MyTimer", true);
int start2;
int i = 0;
NtpV3Packet message;
Date time;
public NtpUpdate() {
}
class MyTask extends TimerTask {
public void run(){
new LongOperation().execute("");
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
myTimer.scheduleAtFixedRate(new MyTask(), 0, 2000);
return Service.START_STICKY;
}
@Override
public void onDestroy(){
myTimer.cancel();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return null;
}
public int start1(){
return start2;
}
}
class LongOperation extends AsyncTask<String, Void, String> {
String server_name = MainActivity.getservername();
TimeInfo timeInfo;
long currentTime;
InetAddress inetAddress;
NtpV3Packet message;
long serverTime,diff;
Date time;
Date currentTime1;
String time1,time2,time3,differene;
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss_SSS");
@Override
protected String doInBackground(String... params) {
NTPUDPClient timeClient = new NTPUDPClient();
try {
inetAddress = InetAddress.getByName(server_name);
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
timeInfo = timeClient.getTime(inetAddress);
Log.d("NTPLOG","timeinfo: " + timeInfo);
message = timeInfo.getMessage();
serverTime = message.getTransmitTimeStamp().getTime();
time = new Date(serverTime);
currentTime = System.currentTimeMillis();
currentTime1 = new Date(Long.parseLong(Long.toString(currentTime)));
Log.d("NTPLOG","timeinfo: " + time);
time1 = dateFormat.format(time);
time2 = dateFormat.format(currentTime);
diff = time.getTime() - currentTime1.getTime();
differene = Long.toString(diff);
time3 = time1 + ";;;;" + time2 + ";;;;" + differene;
Log.d("NTPLOG","timeinfo: " + time1);
} catch (IOException e) {
e.printStackTrace();
}
return time3;
}
@Override
protected void onPostExecute(String result) {
MainActivity.updateui(result);
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
谢谢