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());