1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
amit
  • 21
  • 1
  • 3

2 Answers2

3

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

Community
  • 1
  • 1
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 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. – amit Mar 02 '14 at 14:19
  • The first link is dead, as it's links to "malicious" group according to google groups. – HopefullyHelpful May 04 '17 at 11:45
0

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());
Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • To run a service every one second I tried 2 things. One was to run an infinite loop with a sleep of 1 sec and other was to use Alarm Manager to start my service every second.In the first method when I kill the activity started by the service,then my service also stopped.And i have read that we should use alarm manager only if recurring interval is more than 10 minutes.So how should I repeat my service every second. Also will it be good to keep an infinite loop with a sleep of 1 second – amit May 09 '14 at 13:48