1

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

4

6 回答 6

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
    }
}
于 2012-06-20T11:50:56.987 回答
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();
    }
}
于 2014-02-17T15:06:45.320 回答
1

使用AccessibilityService

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

我在 Android 2.2 (API 8) 到 Android 7.1 (API 25) 中测试和工作。

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() {

    }
}
于 2018-01-04T07:20:01.253 回答
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();
        }
    }
于 2014-06-18T12:04:39.817 回答
-1

有了这个,您可以获得当前的应用程序名称

 Resources appR = getApplicationContext().getResources(); 
 CharSequence txt = appR.getText(appR.getIdentifier("app_name","string", getApplicationContext().getPackageName()));
 System.out.println(txt+"  APp Name");
于 2012-06-20T11:49:29.160 回答
-1

只需使用此代码

getActivity().getApplicationInfo().packageName

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

于 2014-11-21T03:30:45.693 回答