1、监控输出日志接口
/**
*
* 监控输出日志接口
*/
public interface LogcatObserver {
/**
*
* @param info
* 输出的日志信息
*/
public void handleLog(String info);
}
2、继承一个服务
public class LogcatScannerService extends Service implements LogcatObserver {
private String iMEI, simNo, operatorName, simSerialNumber, iMSI, iSO, oSInfo, module, manufacturer, resolution, apps, packageName, versionName,
versionCode;
private DisplayMetrics dm;
private String SourceIdentity = "";// 市场标识
ApplicationInfo appInfo;
private RequestQueue mRequestQueue;
JSONObject params = new JSONObject();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
new AndroidLogcatScannerThread(this).start();
getUserInfo();
}
@Override
public void handleLog(String info) {
// 如果包含了卸载日志信息时就执行此操作
if (info.contains("android.intent.action.DELETE") && info.contains(getPackageName())) {
long t1 = System.currentTimeMillis();
// getUserInfo();
long t2 = System.currentTimeMillis();
doRequest();
long t3 = System.currentTimeMillis();
System.out.println("总时间==" + (t3 - t1));
System.out.println("请求网络时间==" + (t3 - t2));
}
}
/**
*
* @author 实现输出日志信息的监控
*
*/
private class AndroidLogcatScannerThread extends Thread {
private LogcatObserver mObserver;
public AndroidLogcatScannerThread(LogcatObserver observer) {
mObserver = observer;
}
@Override
public void run() {
String[] cmds = { "logcat", "-c" };
String shellCmd = "logcat";
Process process = null;
InputStream is = null;
DataInputStream dis = null;
String line = "";
Runtime runtime = Runtime.getRuntime();
try {
mObserver.handleLog(line);
int waitValue;
waitValue = runtime.exec(cmds).waitFor();
mObserver.handleLog("waitValue=" + waitValue + "\n Has do Clear logcat cache.");
process = runtime.exec(shellCmd);
is = process.getInputStream();
dis = new DataInputStream(is);
while ((line = dis.readLine()) != null) {
if (mObserver != null)
mObserver.handleLog(line);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException ie) {
} finally {
try {
if (dis != null) {
dis.close();
}
if (is != null) {
is.close();
}
if (process != null) {
process.destroy();
}
} catch (Exception e) {
}
}
}
}
/**
* 获取用户信息
*
*
*/
private void getUserInfo() {
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
iMEI = tm.getDeviceId();
simNo = tm.getLine1Number();
operatorName = tm.getNetworkOperatorName();
simSerialNumber = tm.getSimSerialNumber();
iMSI = tm.getSubscriberId();
iSO = tm.getNetworkCountryIso();
oSInfo = android.os.Build.VERSION.RELEASE;
module = android.os.Build.MODEL;
manufacturer = android.os.Build.MANUFACTURER;
try {
appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
SourceIdentity = appInfo.metaData.get("BaiduMobAd_CHANNEL").toString();
PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);
packageName = info.packageName;
versionName = info.versionName;
versionCode = getResources().getString(R.string.version);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
try {
params.put("SourceIdentity", SourceIdentity);
params.put("IMEI", iMEI);
params.put("SimNo", simNo);
params.put("OperatorName", operatorName);
params.put("SimSerialNumber", simSerialNumber);
params.put("IMSI", iMSI);
params.put("OSInfo", oSInfo);
params.put("ISO", iSO);
params.put("Module", module);
params.put("Manufacturer", manufacturer);
params.put("Resolution", resolution);
params.put("apps", apps);
params.put("PackageVersionName", versionName);
params.put("PackageVersionCode", versionCode);
params.put("PackageName", packageName);
params.put("WifiMac", DeviceInfo.getMacAddress(this));
params.put("BluetoothMac", DeviceInfo.getBluetoothAddress(this));
params.put("GuidData", DeviceInfo.filesDirGuid(this));
params.put("GuidSD", DeviceInfo.externalStorageGuid(this));
System.out.println("params=" + params.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
* 在用户点击卸载时 发送数据
* Config为提交的服务器url
* @return
*/
public boolean doRequest() {
boolean isRequestOK = false;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, Config, params,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
System.out.println("argo=======" + arg0);
}
}, new Response.ErrorListener<JSONObject>() {
@Override
public void onErrorResponse(VolleyError error, JSONObject historyCache) {
}
});
mRequestQueue = Volley.newRequestQueue(this);
mRequestQueue.add(jsonObjectRequest, this);
isRequestOK = true;
return isRequestOK;
}
}
3、在manifest.xml注册服务
4、启动服务
startService(new Intent("com.lapel.uninstallservice.LogcatScannerService"));