What I want: I am trying to create an app protector. When someone opens other apps, I want the user to enter the password/PIN.
SO for this, I am trying to read logs as suggested here: (https://stackoverflow.com/a/7239840/1640009)
Foreground Service:
@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
if (intent!=null){
if (intent.getAction()!=null && intent.getAction().equals(Constants.ACTION.STOP_ACTION)) {
killPermissionService();
return START_STICKY;
}
}
if (timer != null) {
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
String logs= LogsUtil.readLogs().toString();
if (logs.contains("START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME]")){
Log.e(TAG,"app launched");
}
} catch (Exception ex) {
Log.e("here","line 166");
ex.printStackTrace();
}
}
}, 1000, 1000);
}
return START_STICKY;
}
// In LogUtil, I have the function
public static StringBuilder readLogs() {
StringBuilder logBuilder = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat *:D -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
logBuilder.append(line + "\n");
}
} catch (IOException e) {
Log.e("error while","fetching log");
e.printStackTrace();
}
return logBuilder;
}
But if conditions never met. I also tried to execute "./adb logcat *:D" directly on the terminal, In the output same line exists when app launch.