【问题标题】:Getting the Foreground Activity of any application in a Service written in other application在其他应用程序中编写的服务中获取任何应用程序的前台活动
【发布时间】:2015-06-10 15:31:14
【问题描述】:

我想要的是我需要对位于前台的任何应用程序的当前 Activity 的引用。

基本上我已经编写了一个服务,我想获得对该服务中“当前”前台活动的引用。 “当前”是指除启动该服务的应用程序活动之外的任何应用程序活动。

【问题讨论】:

    标签: android android-intent android-activity service intentservice


    【解决方案1】:

    我想要的是我需要对位于前台的任何应用程序的当前 Activity 的引用。

    这是不可能的。其他应用程序正在其他进程中运行;您无权访问这些进程中的 Java 对象,例如 Activity 实例。

    【讨论】:

      【解决方案2】:

      这段代码对我有用:

      ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);    
      List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
      Log.d("App", tasks.get(0).processName);
      

      【讨论】:

      • 这只允许访问有关当前进程的信息。但我需要对当前处于前台的活动的引用。
      • 是的。就像@CommonsWare 所说的那样,这不可能是我想要的。我同意他的看法。不过感谢您的回答!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2016-11-12
      相关资源
      最近更新 更多