Android:如何在屏幕上获取当前打开的应用程序名称

时间:2012-06-20 11:40:02

标签: android

请建议我如何获取当前打开的应用程序名称,即使设备上有主屏幕,我也会找到"主屏幕已打开"。

6 个答案:

答案 0 :(得分:10)

你去吧

ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while (i.hasNext()) {
    ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
    try {
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        info.processName, PackageManager.GET_META_DATA));
        Log.w("LABEL", c.toString());
    } catch (Exception e) {
        // Name Not FOund Exception
    }
}

答案 1 :(得分:1)

您还可以使用以下代码列出正在运行的任务:

ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
List li = am.getRunningTasks(100);
Iterator i = li.iterator();
PackageManager pm = getApplicationContext().getPackageManager();
while (i.hasNext()) {
    try {
        ActivityManager.RunningTaskInfo info = (ActivityManager.RunningTaskInfo)(i.next());
        String ac = info.baseActivity.getPackageName();
        CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
        ac, PackageManager.GET_META_DATA));
        Log.v("asd", c.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

答案 2 :(得分:0)

对我来说,上面的例子不起作用。所以我最终使用了这个:

ActivityManager am = (ActivityManager) this
            .getSystemService(ACTIVITY_SERVICE);

    List<ActivityManager.RecentTaskInfo> l = am.getRecentTasks(1,
            ActivityManager.RECENT_WITH_EXCLUDED);
    Iterator<ActivityManager.RecentTaskInfo> i = l.iterator();

    PackageManager pm = this.getPackageManager();

    while (i.hasNext()) {
        try {
            Intent intent = i.next().baseIntent;
            List<ResolveInfo> list = pm.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);

            CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(
                    list.get(0).activityInfo.packageName,
                    PackageManager.GET_META_DATA));

            Toast.makeText(this, "Application name: " + c.toString(),
                    Toast.LENGTH_LONG).show();

        } catch (Exception e) {
            Toast.makeText(this,
                    "Application name not found: " + e.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }

答案 3 :(得分:0)

使用 AccessibilityService

  • 您可以使用AccessibilityService检测当前活动的窗口。
  • 在onAccessibilityEvent回调中,检查TYPE_WINDOW_STATE_CHANGED事件类型以确定当前窗口何时更改。
  • 通过调用PackageManager.getActivityInfo()。
  • 检查窗口是否为活动

我通过Android 7.1(API 25)测试并使用Android 2.2(API 8)。

public class MyAccessibilityService extends AccessibilityService {
    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();

        //Configure these here for compatibility with API 13 and below.
        AccessibilityServiceInfo config = new AccessibilityServiceInfo();
        config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
        config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

        if (Build.VERSION.SDK_INT >= 16)
            //Just in case this helps
            config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

        setServiceInfo(config);
    }
    @Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.d("ABC-",accessibilityEvent.getPackageName()+" -- "+accessibilityEvent.getClassName());
        if (accessibilityEvent.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            if (accessibilityEvent.getPackageName() != null && accessibilityEvent.getClassName() != null) {
                ComponentName componentName = new ComponentName(
                        accessibilityEvent.getPackageName().toString(),
                        accessibilityEvent.getClassName().toString()
                );

                ActivityInfo activityInfo = tryGetActivity(componentName);
                boolean isActivity = activityInfo != null;
                if (isActivity)
                    Log.i("CurrentActivity", componentName.flattenToShortString());
            }
        }
    }

    private ActivityInfo tryGetActivity(ComponentName componentName) {
        try {
            return getPackageManager().getActivityInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            return null;
        }
    }
    @Override
    public void onInterrupt() {

    }
}

答案 4 :(得分:-1)

使用此功能,您可以获取当前的应用程序名称

 Resources appR = getApplicationContext().getResources(); 
 CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
 System.out.println(txt+"  APp Name");

答案 5 :(得分:-1)

只需使用此代码

getActivity().getApplicationInfo().packageName

它为您提供应用程序信息,然后调用包名称。