Android Print Log on screen

Source: Internet
Author: User

Android development needs to quickly locate the problem, print log on the Android screen, is a good channel

Basic idea: Start Logservice read the specified log, use WindowManager to display on the screen

Directly on the code

public class Logservice extends Service {

Private ListView ListView;
Private linkedlist<logline> loglist = new linkedlist<logline> ();
Private Logadapter Madapter;
Private final int max_line = 500;
Private SimpleDateFormat Logcat_time_format = new SimpleDateFormat ("HH:mm:ss. SSS ");
Private Thread Readlog;
Private Boolean isallowreadlog = false;

@Override
Public IBinder Onbind (Intent arg0) {
return null;
}

@Override
public int Onstartcommand (Intent Intent, int flags, int startid) {

Utility.log_tag for the custom logstring,service reads this LOG

Readlog = new Thread (new Logreaderthread (Utility.log_tag));
Readlog.start ();
Createsystemwindow ();
Isallowreadlog = true;
return start_sticky;
}

@Override
public void OnDestroy () {
Removesystemwindow ();
Isallowreadlog = false;
Super.ondestroy ();
}

private void Createsystemwindow () {
Final Windowmanager.layoutparams LP = new Windowmanager.layoutparams (ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, pixelformat.translucent);
Lp.gravity=gravity.left| Gravity.top; Adjust the hover window to the upper left corner
Set the X, Y initialization as the origin in the upper-left corner of the screen.
lp.x=0;
lp.y=0;
Final Layoutinflater inflator = (layoutinflater) getsystemservice (Context.layout_inflater_service);
Final WindowManager wm = (WindowManager) getsystemservice (Window_service);
ListView = (ListView) inflator.inflate (R.layout.log_window, NULL);
Loglist = new linkedlist<logline> ();
Madapter = new Logadapter (this, loglist);
Listview.setadapter (Madapter);
if (Isallowreadlog) {
Wm.addview (listview, LP);
}

}

private void Removesystemwindow () {
if (ListView! = null && listview.getparent () = null) {
Final WindowManager wm = (WindowManager) getsystemservice (Window_service);
Wm.removeviewimmediate (listview);
}
}

Class Logadapter extends Arrayadapter<logline> {

Private Layoutinflater inflator = (layoutinflater) getsystemservice (Context.layout_inflater_service);

Public Logadapter (context context, list<logline> objects) {
Super (context, 0, objects);
}

public void Add (Logline line) {
Loglist.add (line);
Notifydatasetchanged ();
}

@Override
Public logline getItem (int position) {
return Loglist.get (position);
}

@Override
public int GetCount () {
return Loglist.size ();
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
Logline line = GetItem (position);
Viewholder Holder;
if (Convertview = = null) {
Holder = new Viewholder ();
Convertview = Inflator.inflate (R.layout.log_line, parent, false);
Holder.time = (TextView) Convertview.findviewbyid (r.id.log_time);
Holder.content = (TextView) Convertview.findviewbyid (r.id.log_content);
Convertview.settag (holder);
} else {
Holder = (viewholder) convertview.gettag ();
}
Holder.time.setText (Line.time);
Holder.content.setText (line.content);
if (Line.color! = 0) {
Holder.content.setTextColor (Line.color);
} else {
Holder.content.setTextColor (Getresources (). GetColor (Android. R.color.white));
}
return convertview;
}

}

Class Viewholder {
public TextView time;
public TextView content;
}

Class Logreaderthread implements Runnable {

private String filter;

Public Logreaderthread (String filter) {
This.filter = filter;
}

@Override
public void Run () {
Process mlogcatproc = null;
BufferedReader reader = null;
try {
Mlogcatproc = Runtime.getruntime (). EXEC (new string[] {"Logcat", filter + "*:s"});
reader = new BufferedReader (New InputStreamReader (Mlogcatproc.getinputstream ()));
String Line;

while (Isallowreadlog) {
if (line = Reader.readline ()) = null) {
Message msg = new Message ();
Msg.obj = line;
Handler.sendmessage (msg);
}
}
} catch (Exception e) {
E.printstacktrace ();
}
}
}

private void Buildlogline (String line) {
Logline log = new Logline ();
Log.time = Logcat_time_format.format (New Date ()) + ":";
if (Line.startswith ("I")) {
Log.color = Color.parsecolor ("#008f86");
} else if (Line.startswith ("V")) {
Log.color = Color.parsecolor ("#fd7c00");
} else if (Line.startswith ("D")) {
Log.color = Color.parsecolor ("#8f3aa3");
} else if (Line.startswith ("E")) {
Log.color = Color.parsecolor ("#fe2b00");
}
if (Line.contains (")")) {
line = line.substring (Line.indexof (")") + 1, line.length ());
}
Log.content = line;

while (Loglist.size () > Max_line) {
Loglist.remove ();
}
Madapter.add (log);
}

Private Handler Handler = new Handler () {
public void Handlemessage (Message msg) {
Buildlogline (Msg.obj.toString ());
};
};

}

Entity class

public class Logline {

public String time;
public String content;
public int color;

}

Start Server,log will output to the screen, Android share!

Android Print Log on screen

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.