This article describes the Android development approach to getting logcat log information in a program. Share to everyone for your reference, specific as follows:
Sharing a practical example in software development today is a small example I used in the last few days of overtime to monitor log information in a program.
Why do you say it's practical? The reason is that there are a lot of differences between mobile phones and mobile phones after Android developers have changed. For example, the M9 mobile phone development If the project involves access to the mobile phone system, such as access to the system SMS Library, M9 mobile phone It will prompt a dialog box to allow users to choose their own access or not. This brings a great deal of trouble to development adaptation. I was able to get the data directly from here, but now I need to listen to the user's choice? Future Millet mobile phone will certainly have this problem ~ tragedy Ah ~ ~ finally here I choose to use the listening log information to monitor the user click button authorization and not authorized.
1. Get Mobile phone model information
Get the model name
Android.os.Build.MODEL
//Get SDK information
Android.os.Build.VERSION.SDK/
/Get version number
Android.os.Build.VERSION.RELEASE
So it can be written in code
if (Android.os.Build.MODEL.equals ("Meizu_m9")) {
System.out.println ("I am M9 cell phone");
}
2.Logcat description
Android development in a total of 5 log information filters are VERBOSE, DEBUG, INFO, WARN, ERROR
Please look at the following code, the contents of a button to monitor the click event once clicked output a section of Logcat information, in order to monitor the system print this log information we open a thread in the background to monitor it.
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.check);
/** to get this button object **/button
= (button) Findviewbyid (r.id.button0);
/** listens to this button **/
button.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick view {
/** output section of Log information **/
log.i ("Mytest", "This is a Test");
/** open thread is used to monitor log output **/
new Thread (checkactivity.this). Start ();}}
After the thread is opened, runtime is mainly used for filtering logcat information, which is mainly about the parameters inside.
"Logcat" Needless to say, we just want to monitor it hehe.
The "Mytest" is an example of the monitor's tag, which takes the log information output by clicking the button above.
"I" indicates the type of log that is listening, and of course there are other types to be written. VERBOSE (v), DEBUG (d), INFO (i), WARN (W), ERROR (e), but the need to be with the listener and tag one by one symmetry.
"*:s" means to listen for all the information, which means that all logs with the Logcat type I will be fetched as long as the tag is mytest.
All filtered log information is then invoked in Bufferreader ReadLine () to obtain log information for each row.
Line.indexof ("This is a test") if greater than or equal to 0 indicates that the currently fetched log information contains the button we clicked above.
This way you can monitor a variety of log whether we write or the system can be monitored to hear wow ka ~ ~
Finally use toast to show the content.
@Override public
Void Run () {
Process mlogcatproc = null;
BufferedReader reader = null;
try {
//Get Logcat log information
Mlogcatproc = Runtime.getruntime (). EXEC (new string[] {"Logcat", "Mytest:i *:s"});
reader = new BufferedReader (New InputStreamReader (Mlogcatproc.getinputstream ()));
String Line;
while (line = Reader.readline ())!= null) {
if Line.indexof (' This is a test ') > 0} {
//logcat print information can be heard here
//uses Looper to give the interface a display
looper.prepare ();
Toast.maketext (This, "log information is heard", Toast.length_short). Show ();
Looper.loop ();}}
catch (Exception e) {
e.printstacktrace ();
}
}
The most important thing is to read the system log permissions Oh, otherwise it will not be monitored.
<uses-permission android:name= "Android.permission.READ_LOGS"/>
Effect Chart:
Full instance code click here to download the site.
More interested readers of Android-related content can view this site: Android Development Primer and Advanced tutorial, Android resource Operation tips Summary, Android View view tips Summary and Android control usage summary
I hope this article will help you with the Android program.