我想要什么:我正在尝试创建一个应用保护程序。当有人打开其他应用程序时,我希望用户输入密码/ PIN码。
为此,我正在尝试按照此处的建议阅读日志:(https://stackoverflow.com/a/7239840/1640009)
前台服务:
@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;
}
//在LogUtil中,我具有功能
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;
}
但是如果条件从未满足。我还尝试直接在终端上执行“ ./adb logcat *:D”,在应用启动时,输出中存在同一行。