Java 如何检测用户何时启动另一个应用程序

Java 如何检测用户何时启动另一个应用程序,java,android,service,broadcastreceiver,Java,Android,Service,Broadcastreceiver,我正在创建一个应用程序锁。我想检测用户何时启动另一个应用程序,以便向用户显示我的锁屏。现在,我可以针对特定操作打开锁屏,即当用户在屏幕上时。当屏幕状态改变时,我的服务将运行并检查前台应用程序。如果该应用程序在我已阻止的应用程序列表中,则会出现锁定屏幕。但我想在用户每次启动另一个应用程序时启动这项服务。下面是我的BroadcastReceiver类的代码 public void onReceive(Context context, Intent intent) { if (Intent.ACT

我正在创建一个应用程序锁。我想检测用户何时启动另一个应用程序,以便向用户显示我的锁屏。现在,我可以针对特定操作打开锁屏,即当用户在屏幕上时。当屏幕状态改变时,我的服务将运行并检查前台应用程序。如果该应用程序在我已阻止的应用程序列表中,则会出现锁定屏幕。但我想在用户每次启动另一个应用程序时启动这项服务。下面是我的BroadcastReceiver类的代码

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

您无法检测某个应用程序是否在某个特定时间启动,但如果有任何其他应用程序在前台,您肯定可以发现什么

 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;
}
这意味着所有人都在使用它,但似乎程序启动了,之后我们的程序(应用程序保护器)将启动并出现

使用以下代码:

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();
}
注意:-第三方应用程序不能使用systen权限读取日志,因为安全原因高于Android 4.1 请在这里阅读更多内容,
1是您的最佳选择。

查看此链接以了解有关您的问题的更多信息:和此:我正在获取前景中的应用程序。e、 g当我从睡眠模式打开屏幕时。它会检查当前打开的应用程序。我需要知道如何知道用户何时启动应用程序,以便显示锁定屏幕。playstore上有很多类似的应用程序。所以,我想知道他们是如何工作的。我有同样的问题。。我在quora发现了这一点,如果你坚持的话,我会专门研究这一点。我有一个建议。。如果前景中可用的应用程序数量增加了一个,则您可以不断签入bg线程(如果没有),这意味着一个新的应用程序被启动了。我非常感谢目前我能得到的任何帮助,因为我在过去3天一直在开发这个应用程序,但没有找到解决方案。请给我一些时间。同时,你也可以看看我最后的评论。
 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();
}