On Nov 27, 2012, at 4:42 PM, Stephan Steiner <[email protected]> wrote:
> However, the second link you gave mentions that your need to have rooted your
> phone to do so
_One_ of the answers did so, but not the topmost answer, which just states that
you need to have the android.permission.READ_LOGS permission.
I've verified that this is the case, at least on Android 4.2 Jelly Bean; my
device is unrooted, my app has the READ_LOGS permission, and I'm able to
execute logcat. (Source is mostly the default template, with a <TextView/>
added after the <Button/> in Main.axml with an id of 'logcat'.)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Scratch.Logcat
{
[Activity (Label = "Scratch.Logcat", MainLauncher = true)]
public class Activity1 : Activity
{
int count = 1;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Console.WriteLine ("Scratch.Logcat");
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
int logcatLines = 0;
using (var logcat = FindViewById<TextView> (Resource.Id.logcat))
Exec (new[]{"/system/bin/logcat", "-d"},
(o, e) => {
if (logcatLines++ > 10)
return;
Console.WriteLine (e.Data);
logcat.Append ((e.Data ?? "") + "\n");
if (logcatLines == 10) {
var p = (System.Diagnostics.Process) o;
p.CancelErrorRead ();
p.CancelOutputRead ();
if (!p.HasExited) {
try {
p.Kill ();
} catch (Exception ex) {
Console.WriteLine ("Unable to kill:
{0}", ex.Message);
}
}
}
},
(o, e) => {
Console.Error.WriteLine (e.Data);
}
);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += delegate {
button.Text = string.Format ("{0} clicks!", count++);
};
}
static int Exec (string[] command,
EventHandler<DataReceivedEventArgs> stdout, EventHandler<DataReceivedEventArgs>
stderr)
{
var psi = new ProcessStartInfo (command [0], '"' + string.Join
("\" \"", command.Skip (1)) + '"') {
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
var p = new System.Diagnostics.Process () {
StartInfo = psi,
EnableRaisingEvents = true,
};
if (stdout != null)
p.OutputDataReceived += new DataReceivedEventHandler
(stdout);
if (stderr != null)
p.ErrorDataReceived += new DataReceivedEventHandler
(stderr);
using (p) {
p.Start ();
if (stdout != null)
p.BeginOutputReadLine ();
if (stderr != null)
p.BeginErrorReadLine ();
p.WaitForExit ();
return p.ExitCode;
}
}
}
}
- Jon
_______________________________________________
Monodroid mailing list
[email protected]
UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid