In the same situation as before, I wanted to know the details of the application log regarding the broadcast receiver, running services or application crashes, etc., when it was used by a user on an Android device.
[therefore, using this code, you can get information, for example, when your broadcast receiver starts, stops, crashes, etc.]
I wrote my own LogCat and it works well, and here the code can help you
First you need to put some constants in a persistent application file
public static final String LOG_DIR="/SeedSpeak/LOG"; public static final String LOG_MODE_EXCEPTION="exception"; public static final String LOG_MODE_INFO="info";
Then you need to create three methods in the Util application file
public static void MyLog(String logMode, String msgKey, Object msgValue) { File sdCard = Environment.getExternalStorageDirectory(); File dir = new File(sdCard.getAbsolutePath()+ SeedSpeakConstants.LOG_DIR); if (!dir.exists()) { dir.mkdirs(); } String logFileAbsPath = dir + File.separator + "SeedSpeakLog.txt"; BufferedWriter bufferedWritter = null; try { bufferedWritter = new BufferedWriter( new FileWriter(logFileAbsPath, true)); String logValue = null; if (logMode.equalsIgnoreCase( SeedSpeakConstants.LOG_MODE_EXCEPTION)) { logValue = logStackTrace((Throwable) msgValue); } else { logValue = msgValue.toString(); } logValue = currentDateTimeValue() + ": " + logMode + " :" + msgKey + ": " + logValue + "\n"; bufferedWritter.write(logValue); bufferedWritter.newLine(); bufferedWritter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static String logStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw, true); t.printStackTrace(pw); pw.flush(); sw.flush(); return sw.toString(); } public static String currentDateTimeValue() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd-HH:mm:ss"); formatter.setLenient(false); Date curDate = new Date(); String curTime = formatter.format(curDate); return curTime; }
Now, how to use this in your project is so simple :)
Note: here TAG is just the current file (activity / service / BR name), like this
private final static String TAG = "PlantSeedActivity";
(1). to print only information (BR, the service is running or not .. bla bla) log use this
SeedSpeakUtill.MyLog(SeedSpeakConstants.LOG_MODE_INFO, TAG + ".onCreate", "Service is started now");
(2). for print exception / failure information use this method
try{ // todo here } catch (Exception ex) { SeedSpeakUtill.MyLog(SeedSpeakConstants.LOG_MODE_EXCEPTION, TAG + ".onStartCommand", ex); }
Now you just put the application on the Android device and when you want to get the log data, go to the SeedSpeakLog.txt file and debug the application.
or another way, I heard that Google provice library ACRA (Application Crash Report for Android)
you may like this and this link
PL give me a comment if you have any problems reorganizing this issue.