You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术问询:如何获取用户打开任意应用时的package name或应用名称?

Hey there! I totally get needing to grab the package name or app name when any app is launched—super useful for automation, debugging, or just keeping track of app usage. Let me break down how to do this across different platforms with practical examples:

Android Platform

This is the most straightforward to implement since Android provides official APIs for listening to app launches.

Method 1: Accessibility Service (Background Listening)

The Accessibility Service is the official way to monitor system-wide app events, including when an app is brought to the foreground. Here's a step-by-step example:

  1. Create an Accessibility Service class
class AppLaunchMonitorService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent) {
        // We care about events where an app gains focus (launched or brought to foreground)
        if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            val packageName = event.packageName?.toString()
            
            // Get the human-readable app name using PackageManager
            packageName?.let { pkg ->
                val appLabel = packageManager.getApplicationLabel(
                    packageManager.getApplicationInfo(pkg, 0)
                ).toString()
                println("Launched App: $appLabel | Package Name: $pkg")
            }
        }
    }

    override fun onInterrupt() {
        // Handle service interruption here (e.g., log a message)
    }
}
  1. Register the service in AndroidManifest.xml
    You need to declare the service and request necessary permissions:
<service
    android:name=".AppLaunchMonitorService"
    android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
    <intent-filter>
        <action android:name="android.accessibilityservice.AccessibilityService" />
    </intent-filter>
    <meta-data
        android:name="android.accessibilityservice"
        android:resource="@xml/accessibility_service_config" />
</service>
  1. Create the accessibility service config file
    Add res/xml/accessibility_service_config.xml with:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true" />
  1. Request user permission
    Users need to manually enable accessibility permissions for your app. You can redirect them to the settings page:
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))

Method 2: ADB Command (For Debugging)

If you just need to check the current foreground app quickly during development, use this ADB command:

adb shell dumpsys window | grep mCurrentFocus

The output will include the package name (e.g., com.example.myapp from mCurrentFocus=Window{... u0 com.example.myapp/com.example.myapp.MainActivity}).

iOS Platform

iOS has stricter sandbox restrictions, so monitoring arbitrary app launches depends on whether the device is jailbroken:

Jailbroken Devices

You can use MobileSubstrate (Cydia Substrate) to hook into system methods that trigger when apps launch. Here's a simple example using Logos syntax:

%hook UIApplication
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSString *bundleId = application.bundleIdentifier;
    NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
    NSLog(@"Launched App: %@ | Bundle ID: %@", appName, bundleId);
    return %orig; // Call the original method to not break app functionality
}
%end

Compile this into a tweak and install it via Cydia to start monitoring.

Non-Jailbroken Devices

Unfortunately, Apple's sandbox prevents apps from monitoring other apps' launches. The only way to get app info is if you're launching the app yourself via openURL, but you can't listen to arbitrary launches from other apps.

Bonus: Desktop Platforms

If you're targeting desktop apps:

  • Windows: Use PowerShell to get active app details:
Get-Process | Where-Object {$_.MainWindowTitle -ne ""} | Select-Object ProcessName, MainWindowTitle
  • macOS: Use this terminal command to list running apps with their bundle IDs:
osascript -e 'tell application "System Events" to get the bundle identifier of every process whose background only is false'

Hope these solutions work for you! If you're targeting a specific platform or need more details on any of these methods, feel free to ask follow-up questions.

内容的提问来源于stack exchange,提问作者Trung Đoan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:04:51