我正在创建一个应用程序锁。我想要检测用户何时启动另一个应用程序,以便我可以向用户显示我的锁定屏幕。现在,我可以针对特定的操作打开锁定屏幕,即当用户在屏幕上时。当屏幕状态改变时,我的服务运行并检查前台应用程序。如果该应用程序在我已阻止应用程序列表中,则会出现锁定屏幕。但我想在每次用户启动另一个应用程序时启动服务。以下是我的BroadcastReciever类的代码
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction()))
{
Intent service = new Intent(context, BackgroundService.class);
context.startService(service);
Intent service1 = new Intent(context, BackgroundService.class);
context.stopService(service1);
}发布于 2020-11-05 15:01:11
您无法检测某个应用程序是否在特定时间启动,但您可以确定是否有其他应用程序处于前台。
public static boolean isForeground(Context ctx, String myPackage){
ActivityManager manager = (ActivityManager)
ctx.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > runningTaskInfo =
manager.getRunningTasks(1);
ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equals(myPackage)) {
return true;
}
return false;
}要检测应用程序是否在内存中,无论是在前台还是后台,请使用以下命令: ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);List runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for")
{
// Do you stuff
}
}您可以考虑启动BG检查并尝试找出,如果没有。前台的应用程序增加了,这意味着一个新的应用程序已经启动。
选项2:
我认为我们可以使用logcat并分析它的输出。
在所有类似的程序中,我都找到了这个权限:
android.permission.READ_LOGS这意味着所有人都在使用它,但似乎程序启动了,在那之后我们的程序(应用程序保护器)将启动并带到前面。
使用以下代码:
try
{
Process mLogcatProc = null;
BufferedReader reader = null;
mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});
reader = new BufferedReader(new
InputStreamReader(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null)
{
log.append(line);
log.append(separator);
}
String w = log.toString();
Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}注意:-出于安全原因,第三方应用程序不能使用系统权限来读取日志上面的Android 4.1阅读更多在这里,https://commonsware.com/blog/2012/07/12/read-logs-regression.html.Option 1是你最好的选择。
https://stackoverflow.com/questions/64692359
复制相似问题