2

ActivityManager.getRunningAppProcesses 将仅返回自 5.1.1 以来的当前应用程序:构建 LMY48L。应用程序需要具有 REAL_GET_TASK 权限才能访问所有应用程序的进程信息。REAL_GET_TASK 仅在 SYSTEM 应用程序中使用。GET_TASK 权限已被弃用。

有人告诉我使用 UsageStatsManager,但 PACKAGE_USAGE_STATS 权限需要 API 23(在https://developer.android.com/reference/android/Manifest.permission.html中说)

并且 UsageStatsManager 是获取正在运行的应用程序的奇怪方式,有没有常见的方法来返回正在运行的应用程序列表?

4

2 回答 2

1

我到处搜索,只找到一个对我有帮助的网站:

android.app.usage.UsageStatsManager 的 Java 代码示例

要获取有关 UsageStatsManager 的更多信息,请阅读:

UsageStatsManager

以下是获取 API 22 及以上版本中正在运行的应用程序包名称的方法:

private String getProcessName() {
    String foregroundProcess = "";
    ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE);
    // Process running
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(USAGE_STATS_SERVICE);
        long time = System.currentTimeMillis();
        // We get usage stats for the last 10 seconds
        List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);
        // Sort the stats by the last time used
        if(stats != null) {
            SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
            for (UsageStats usageStats : stats) {
                mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
                Log.d("RunningAppProcessInfo","Package name : "+usageStats.getPackageName());
            }

        }
    } 
}

在日志中,您将能够看到正在运行的应用程序的包名称...希望对您有所帮助:)

于 2017-07-20T11:32:39.197 回答
0

使用 Rxjava&RxAndroid

private Observable<String> getRunningAppPackageName() {
    return Observable.create(new Observable.OnSubscribe<String>() {
        @Override
        public void call(Subscriber<? super String> subscriber) {
            Process process = null;
            try {
                process = new ProcessBuilder("ps").start();
            } catch (IOException e) {
                subscriber.onError(e);
                return;
            }
            InputStream in = process.getInputStream();
            Scanner scanner = new Scanner(in);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                if (line.startsWith("u0_")) {
                    String[] temp = line.split(" ");
                    String packageName = temp[temp.length - 1];
                    if (packageName.contains("/")) continue;
                    if (packageName.contains(":")) {
                        packageName = packageName.split(":")[0];
                    }
                    subscriber.onNext(packageName);
                }
            }
            subscriber.onCompleted();
            scanner.close();
            process.destroy();
        }
    });
}

getRunningAppPackageName()
            .subscribeOn(Schedulers.io())
            .onBackpressureBuffer()
            .observeOn(Schedulers.io())
            .distinct()
            .map(new Func1<String, PackageInfo>() {
                @Override
                public PackageInfo call(String s) {
                    PackageInfo packageInfo = null;
                    try {
                        packageInfo = packageManager.getPackageInfo(s, PackageManager.GET_PERMISSIONS);
                    } catch (PackageManager.NameNotFoundException e) {
                    }
                    return packageInfo;
                }
            })
            .filter(new Func1<PackageInfo, Boolean>() {
                @Override
                public Boolean call(PackageInfo packageInfo) {
                    return packageInfo != null;
                }
            }).subscribe(new Subscriber<PackageInfo>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                }

                @Override
                public void onNext(PackageInfo packageInfo) {
                    // do something
                }
            });
于 2016-01-22T02:13:10.067 回答