version:1.0
update:2021-10-13 18:52:13 fix:去除okgo,rxAndroid1,优化依赖 add:切换到奥乐云平台
This commit is contained in:
249
app/src/main/java/com/aoleyun/sn/service/LogcatService.java
Normal file
249
app/src/main/java/com/aoleyun/sn/service/LogcatService.java
Normal file
@@ -0,0 +1,249 @@
|
||||
package com.aoleyun.sn.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.IBinder;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.aoleyun.sn.utils.Logutils;
|
||||
import com.aoleyun.sn.utils.Utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class LogcatService extends Service {
|
||||
public final static String LOGCAT_START_ACTION = "START";
|
||||
public final static String LOGCAT_STOP_ACTION = "STOP";
|
||||
private String TAG = LogcatService.class.getSimpleName();
|
||||
|
||||
|
||||
public LogcatService() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
// TODO: Return the communication channel to the service.
|
||||
// throw new UnsupportedOperationException("Not yet implemented");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
logFilePath = getExternalCacheDir() + File.separator + "LOG" + File.separator;
|
||||
File file = new File(logFilePath);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
// try {
|
||||
// exec = Runtime.getRuntime().exec(running);
|
||||
// final InputStream is = exec.getInputStream();
|
||||
// logThread = new LogThread(is);
|
||||
// logThread.start();
|
||||
// // is.close();
|
||||
//// exec.destroy();
|
||||
// } catch (IOException e) {
|
||||
//// e.printStackTrace();
|
||||
// Logutils.e("第一个是Logcat", e.getMessage());
|
||||
// }
|
||||
registerLogcatReceiver();
|
||||
}
|
||||
|
||||
LogcatReceiver receiver;
|
||||
|
||||
private void registerLogcatReceiver() {
|
||||
receiver = new LogcatReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(LOGCAT_START_ACTION);
|
||||
filter.addAction(LOGCAT_STOP_ACTION);
|
||||
registerReceiver(receiver, filter);
|
||||
}
|
||||
|
||||
Thread thread = null;
|
||||
|
||||
private void creatLogThread() {
|
||||
if (thread == null) {
|
||||
thread = new Thread(LogcatRunnale);
|
||||
}
|
||||
}
|
||||
|
||||
private void startLogThread() {
|
||||
if (thread == null) {
|
||||
creatLogThread();
|
||||
shouldGetLog = true;
|
||||
thread.start();
|
||||
} else {
|
||||
//已经有线程正在运行了
|
||||
}
|
||||
}
|
||||
|
||||
private void stopLogThread() {
|
||||
if (thread != null) {
|
||||
shouldGetLog = false;
|
||||
thread = null;
|
||||
//清除adb 缓存
|
||||
try {
|
||||
Runtime.getRuntime().exec(clearLogcat);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Process exec;
|
||||
private LogThread logThread;
|
||||
|
||||
//第一个是Logcat ,也就是我们想要获取的log日志
|
||||
//第二个是 -s 也就是表示过滤的意思
|
||||
//第三个就是 我们要过滤的类型 W表示warm ,我们也可以换成 D :debug, I:info,E:error等等
|
||||
// String[] running = new String[]{"logcat", "-s", "adb logcat *: W"};
|
||||
String[] running = new String[]{"logcat"};
|
||||
String clearLogcat = "logcat -c";
|
||||
String logFilePath;
|
||||
//= getExternalCacheDir() + File.separator + "LOG" + File.separator;
|
||||
String logFileName;
|
||||
|
||||
class LogThread extends Thread {
|
||||
InputStream is;
|
||||
|
||||
LogThread(InputStream inputStream) {
|
||||
super();
|
||||
this.is = inputStream;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
FileOutputStream os = null;
|
||||
try {
|
||||
//新建一个路径信息
|
||||
File file = new File(logFilePath + logFileName);
|
||||
Logutils.e(TAG, "run: " + logFilePath);
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
file.createNewFile();
|
||||
}
|
||||
os = new FileOutputStream(logFilePath);
|
||||
int len = 0;
|
||||
byte[] buf = new byte[1024];
|
||||
while (-1 != (len = is.read(buf))) {
|
||||
os.write(buf, 0, len);
|
||||
os.flush();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Logutils.e("writelog", "read logcat process failed. message: "
|
||||
+ e.getMessage());
|
||||
} finally {
|
||||
if (null != os) {
|
||||
try {
|
||||
os.close();
|
||||
os = null;
|
||||
} catch (IOException e) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Runnable LogcatRunnale = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getLog();
|
||||
}
|
||||
};
|
||||
|
||||
public static boolean shouldGetLog = true;
|
||||
|
||||
private void getLog() {
|
||||
Process process = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(running);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
InputStreamReader inputStreamReader;
|
||||
BufferedReader bufferedReader;
|
||||
FileOutputStream fileOutputStream;
|
||||
File file = new File(logFilePath);
|
||||
Logutils.e(TAG, "getLog: " + logFilePath);
|
||||
if (!file.exists()) {
|
||||
file.mkdirs();
|
||||
}
|
||||
logFileName = Utils.getSerial() + "-" + getTime() + ".log";
|
||||
try {
|
||||
inputStreamReader = new InputStreamReader(process.getInputStream());
|
||||
bufferedReader = new BufferedReader(inputStreamReader);
|
||||
fileOutputStream = new FileOutputStream(logFilePath + logFileName);
|
||||
Logutils.e(TAG, "getLog: " + logFilePath + logFileName);
|
||||
|
||||
String logEntry;
|
||||
while (shouldGetLog) {
|
||||
logEntry = bufferedReader.readLine() + "\n";
|
||||
fileOutputStream.write(logEntry.getBytes());
|
||||
fileOutputStream.flush();
|
||||
}
|
||||
inputStreamReader.close();
|
||||
bufferedReader.close();
|
||||
fileOutputStream.close();
|
||||
Logutils.e(TAG, "getLog: " + "closed");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NullPointerException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
class LogcatReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Logutils.e(TAG, "onReceive: " + action);
|
||||
if (TextUtils.isEmpty(action)) {
|
||||
return;
|
||||
}
|
||||
switch (action) {
|
||||
case LOGCAT_START_ACTION:
|
||||
startLogThread();
|
||||
break;
|
||||
case LOGCAT_STOP_ACTION:
|
||||
stopLogThread();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTime() {
|
||||
long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
Date d1 = new Date(time);
|
||||
String t1 = format.format(d1);
|
||||
return t1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (receiver != null) {
|
||||
unregisterReceiver(receiver);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user