Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我正在使用后台服务或广播接收器,我可以通过它的包名称以某种方式知道当前哪个应用程序在前台吗?提前致谢。
使用ActivityManager。
ActivityManager mManager = (ActivityManager)getSystemService(UpdateService.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> myTasks = activityManager.getRunningTasks(1); ForegroundActivity => myTasks.get(0).topActivity.getPackageName();