Logcat information is always available on Android programs

Source: Internet
Author: User

For example, Meizu M9 mobile phone development If the project involves access to the mobile phone system, such as access to the system SMS Library, M9 phone It will prompt a dialog box to let users choose their own access or not access. This brings great trouble to the development adaptation.  I could have gotten the data right 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 listen to the user click button authorization and not authorized.


Logcat description
Android development in a total of 5 log information filter is VERBOSE, DEBUG, info, WARN, ERROR, these members of the pot friends should know it, do not know to give me a message ha ~ ~
Please observe the following code, the content for listening to a button click event once clicked to output a section of Logcat information, in order to listen to the system print this log message we open a thread in the background to listen to it.


"Logcat" Needless to say, we just want to listen to it hehe. The
"Mytest" indicates that the listener's tag here is an example of the log information output by clicking on the button above. The
"I" represents the log type of the listener, and of course it can write other types. VERBOSE (v), DEBUG (d), INFO (i), WARN (W), ERROR (e), but need to be symmetrical with the monitor and tag one by one. The
"*:s" means listening for all the information, which means that all logs logcat Type I will be retrieved as long as the tag is mytest.

Then all the filtered log information is present in Bufferreader call ReadLine () to get the log information for each row.
Line.indexof ("This is a test") if greater than or equal to 0 means that the currently obtained log information contains the button we clicked above.


[Java]View Plaincopy
  1. Public void onCreate (Bundle savedinstancestate) {
  2. Super  . OnCreate (savedinstancestate);
  3. Setcontentview (R.layout.main);
  4. Button btn = (button) Findviewbyid (R.id.btn_b);
  5. Btn.setonclicklistener (new View.onclicklistener () {
  6. @Override   
  7. Public void OnClick (View v) {
  8. //Intent Intent = new Intent ("Android.intent.action.APP_A_SECOND_ACTIVITY");   
  9. //Intent.setflags (intent.flag_activity_new_task);   
  10. //StartActivity (intent);   
  11. //Toast.maketext (Firstactivity.this, getandroidsdkversion () + "", 1). Show ();   
  12. LOG.I ("Mytest", "This is a 11test");
  13. /** Open thread is used to listen for log output information **/     
  14. New Thread (new Runnable () {
  15. @Override   
  16. Public void run () {
  17. Process Mlogcatproc = null;
  18. BufferedReader reader = null;
  19. Try  {  
  20. //Get Logcat log information   
  21. Mlogcatproc = Runtime.getruntime (). EXEC (new string[] { "Logcat","Mytest:i *:  S " });
  22. reader = new BufferedReader (new InputStreamReader (Mlogcatproc.getinputstream ()))  ;
  23. String Line;
  24. while (line = Reader.readline ()) = null) {
  25. if (Line.indexof ("This is a test") > 0) {
  26. //logcat Printing information can be heard here   
  27. //Use Looper to show a display to the interface   
  28. Looper.prepare ();
  29. Toast.maketext (firstactivity.    this, "Listen to log Message", Toast.length_short). Show ();
  30. Looper.loop ();
  31. }
  32. }
  33. } catch (Exception e) {
  34. E.printstacktrace ();
  35. }
  36. }
  37. }). Start ();
  38. }
  39. });
  40. }


Note: By default, threads do not have a message loop, so call Looper.prepare () to create a message loop for the thread,
    then pass, Looper.loop () to make the message loop work.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.