amit
amit

Reputation: 21

How to read android log cat programmatically for other applications

I have read that you can't read logs of other apps.Also that apps like App Lock constantly read logs to find which apps have started and then present their lock screen.So they must be reading logs completely.

I want to read logs like this for all apps. I/ActivityManager( 585): Starting activity: Intent { action=android.intent.action...}

Do we have to be root users to read logs.

Upvotes: 1

Views: 4803

Answers (2)

Ajay S
Ajay S

Reputation: 48602

As @Raghav said correctly that you can not read the Logcat of other apps from Android 4.1+ but can do before Android 4.1+.

To fetch the logs below Android 4.1+, see below code or you can go through this thread https://stackoverflow.com/a/6228065/1741671

AndroidManifest.xml

<uses-permission android:name="android.permission.READ_LOGS" />

Code

Runtime.getRuntime().exec("logcat -c").waitFor();
Process process = Runtime.getRuntime().exec("logcat -v long *:*");
BufferedReader reader = 
    new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
    String nextLine = reader.readLine();
    if (!nextLine.contains("LogWatcher-D")) {
        Log.w("LogWatcher-D", "See: " + nextLine);
    }

    // Process line
}

but apps like AppLock constantly read logs (i.e they poll) to find which apps have started and then present their own lock screen activity. How do they do so without reading logs OR is there some other way to find out if an app has started.

If you really want to do then you can do simply run your service in a 1-2 seconds to see details of which app have started.

You can do like this and add the permission <uses-permission android:name="android.permission.GET_TASKS"/>

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1); 
Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82553

As of JellyBean, you can no longer read anything in the LogCat that was not put there by your application.

Prior to JellyBean, and post JellyBean for your own app, you can use the Runtime to execute command line arguments to retrieve the logcat

Upvotes: 3

Related Questions