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
- Public void onCreate (Bundle savedinstancestate) {
- Super . OnCreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button btn = (button) Findviewbyid (R.id.btn_b);
- Btn.setonclicklistener (new View.onclicklistener () {
- @Override
- Public void OnClick (View v) {
- //Intent Intent = new Intent ("Android.intent.action.APP_A_SECOND_ACTIVITY");
- //Intent.setflags (intent.flag_activity_new_task);
- //StartActivity (intent);
- //Toast.maketext (Firstactivity.this, getandroidsdkversion () + "", 1). Show ();
- LOG.I ("Mytest", "This is a 11test");
- /** Open thread is used to listen for log output information **/
- New Thread (new Runnable () {
- @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 Printing information can be heard here
- //Use Looper to show a display to the interface
- Looper.prepare ();
- Toast.maketext (firstactivity. this, "Listen to log Message", Toast.length_short). Show ();
- Looper.loop ();
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- }
- }
- }). Start ();
- }
- });
- }
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.