ActivityManager.getRunningAppProcesses将仅返回自5.1.1以来的当前App:Build 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是获取正在运行的应用程序的奇怪方式,是否有任何常用方法可以返回正在运行的应用程序列表?
答案 0 :(得分:1)
我到处搜索,发现只有一个网站帮助了我:
Java Code Examples for android.app.usage.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());
}
}
}
}
在日志中,您将能够看到正在运行的应用程序的包名称...希望它有所帮助:)
答案 1 :(得分:0)
使用Rxjava&amp; 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
}
});