Skip to content

Commit

Permalink
Add: app starting time
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhuangqingbin committed Dec 19, 2013
1 parent 1e26fa7 commit ee187d7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/com/netease/qa/emmagee/activity/MainPageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ public void onClick(View v) {
Intent intent = getPackageManager()
.getLaunchIntentForPackage(packageName);
Log.d(LOG_TAG, packageName);
//清除logcat日志
try {
Runtime.getRuntime().exec("logcat -c");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
startActivity(intent);
} catch (NullPointerException e) {
Expand Down
52 changes: 51 additions & 1 deletion src/com/netease/qa/emmagee/service/EmmageeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
*/
package com.netease.qa.emmagee.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -117,6 +119,12 @@ public class EmmageeService extends Service {
private CurrentInfo currentInfo;
private BatteryInfoBroadcastReceiver batteryBroadcast = null;

//获取启动时间所需变量
private static final int MAX_START_TIME_COUNT = 5; //暂定最大轮询5次
private int getStartTimeCount = 0;
private boolean isGetStartTime = true;
private String startTime = "";

@Override
public void onCreate() {
Log.i(LOG_TAG, "onCreate");
Expand Down Expand Up @@ -351,8 +359,12 @@ public void run() {
if (!isServiceStop) {
dataRefresh();
handler.postDelayed(this, delaytime);
if (isFloating)
if (isFloating) {
windowManager.updateViewLayout(viFloatingWindow, wmParams);
}
//每次刷新数据(有最大次数限制)就尝试获取logcat的启动时间,如果获取到了,就退出
//TODO:是否每个应用都能正常正确的获取
getStartTimeFromLogcat();
} else {
Intent intent = new Intent();
intent.putExtra("isServiceStop", true);
Expand All @@ -363,6 +375,37 @@ public void run() {
}
};

/**
* get start time from logcat
*/
private void getStartTimeFromLogcat() {
if (isGetStartTime && getStartTimeCount < MAX_START_TIME_COUNT) {
try {
String logcatCommand = "logcat -v time -d ActivityManager:I *:S";
Process process = Runtime.getRuntime().exec(logcatCommand);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder strBuilder = new StringBuilder();
String line = "";

while ((line = bufferedReader.readLine()) != null) {
strBuilder.append(line);
strBuilder.append("\r\n");
if (line.matches(".*Displayed.*\\+(.*)ms.*")) {
//TODO:可以通过正则表达式获取,同时获取启动的是哪个Activity
startTime = line.substring(line.lastIndexOf("+") + 1, line.lastIndexOf("ms") + 2);
Toast.makeText(EmmageeService.this, "启动时间:" + startTime, Toast.LENGTH_LONG).show();
isGetStartTime = false;
}
}
getStartTimeCount++;
Log.w(LOG_TAG, "启动日志:" + strBuilder.toString());
Log.w(LOG_TAG, "getStartCount:" + getStartTimeCount);
} catch (IOException e) {
e.printStackTrace();
}
}
}

/**
* refresh the performance data showing in floating window.
*
Expand Down Expand Up @@ -446,6 +489,13 @@ public void onDestroy() {
if (windowManager != null)
windowManager.removeView(viFloatingWindow);
handler.removeCallbacks(task);
//在文件最后把启动时间加上
try {
//TODO:现在把启动时间写在了文件最后
bw.write("\r\n启动时间:" + startTime + "\r\n");
} catch (IOException e1) {
e1.printStackTrace();
}
closeOpenedStream();
isStop = true;
unregisterReceiver(batteryBroadcast);
Expand Down

0 comments on commit ee187d7

Please sign in to comment.