|
|
| |
|
| |
import
android.app.ActivityManager; |
| |
import
android.app.AppOpsManager; |
| |
import
android.app.usage.UsageStats; |
| |
import
android.app.usage.UsageStatsManager; |
| |
import
android.content.Context; |
| |
import
android.content.Intent; |
| |
import
android.content.pm.ApplicationInfo; |
| |
import
android.content.pm.PackageManager; |
| |
import
android.content.pm.ResolveInfo; |
| |
import
android.provider.Settings; |
| |
import
android.support.annotation.NonNull; |
| |
|
| |
import
java.util.Arrays; |
| |
import
java.util.Collections; |
| |
import
java.util.HashSet; |
| |
import
java.util.List; |
| |
import
java.util.Set; |
| |
|
| |
/** |
| |
* <pre> |
| |
* author: Blankj |
| |
* blog : http://blankj.com |
| |
* time : 2016/10/18 |
| |
* desc : 进程相关工具类 |
| |
* </pre> |
| |
*/ |
| |
public
final class
ProcessUtils { |
| |
|
| |
private
ProcessUtils() { |
| |
throw
new UnsupportedOperationException("u can't instantiate me..."); |
| |
} |
| |
|
| |
/** |
| |
* 获取前台线程包名 |
| |
* <p>当不是查看当前App,且SDK大于21时, |
| |
* 需添加权限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p> |
| |
* |
| |
* @return 前台应用包名 |
| |
*/ |
| |
public
static String
getForegroundProcessName() { |
| |
ActivityManager manager
= (ActivityManager)
Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE); |
| |
List<ActivityManager.RunningAppProcessInfo>
infos = manager.getRunningAppProcesses(); |
| |
if (infos
!= null
&& infos.size()
!= 0) { |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
if (info.importance
==
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
{ |
| |
return info.processName; |
| |
} |
| |
} |
| |
} |
| |
if (android.os.Build.VERSION.SDK_INT
>
android.os.Build.VERSION_CODES.LOLLIPOP)
{ |
| |
PackageManager packageManager
=
Utils.getContext().getPackageManager(); |
| |
Intent intent
= new
Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); |
| |
List<ResolveInfo> list
= packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY); |
| |
System.out.println(list); |
| |
if (list.size()
>
0) {// 有"有权查看使用权限的应用"选项 |
| |
try { |
| |
ApplicationInfo info
= packageManager.getApplicationInfo(Utils.getContext().getPackageName(),
0); |
| |
AppOpsManager aom
= (AppOpsManager)
Utils.getContext().getSystemService(Context.APP_OPS_SERVICE); |
| |
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
info.uid, info.packageName)
!=
AppOpsManager.MODE_ALLOWED) { |
| |
Utils.getContext().startActivity(intent); |
| |
} |
| |
if (aom.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
info.uid, info.packageName)
!=
AppOpsManager.MODE_ALLOWED) { |
| |
LogUtils.d("getForegroundApp",
"没有打开\"有权查看使用权限的应用\"选项"); |
| |
return
null; |
| |
} |
| |
UsageStatsManager usageStatsManager
= (UsageStatsManager)
Utils.getContext().getSystemService(Context.USAGE_STATS_SERVICE); |
| |
long endTime
= System.currentTimeMillis(); |
| |
long beginTime
= endTime -
86400000 *
7; |
| |
List<UsageStats> usageStatses
= usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST,
beginTime, endTime); |
| |
if (usageStatses
== null
|| usageStatses.isEmpty())
return
null; |
| |
UsageStats recentStats
= null; |
| |
for (UsageStats usageStats
: usageStatses) { |
| |
if (recentStats
== null
|| usageStats.getLastTimeUsed()
> recentStats.getLastTimeUsed()) { |
| |
recentStats = usageStats; |
| |
} |
| |
} |
| |
return recentStats
== null
? null
: recentStats.getPackageName(); |
| |
} catch (PackageManager.NameNotFoundException
e) { |
| |
e.printStackTrace(); |
| |
} |
| |
} else { |
| |
LogUtils.d("getForegroundApp",
"无\"有权查看使用权限的应用\"选项"); |
| |
} |
| |
} |
| |
return
null; |
| |
} |
| |
|
| |
/** |
| |
* 获取后台服务进程 |
| |
* <p>需添加权限 {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>}</p> |
| |
* |
| |
* @return 后台服务进程 |
| |
*/ |
| |
public
static Set<String>
getAllBackgroundProcesses() { |
| |
ActivityManager am
= (ActivityManager)
Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE); |
| |
List<ActivityManager.RunningAppProcessInfo>
infos = am.getRunningAppProcesses(); |
| |
Set<String> set
=
new HashSet<>(); |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
Collections.addAll(set, info.pkgList); |
| |
} |
| |
return set; |
| |
} |
| |
|
| |
/** |
| |
* 杀死所有的后台服务进程 |
| |
* <p>需添加权限 {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>}</p> |
| |
* |
| |
* @return 被暂时杀死的服务集合 |
| |
*/ |
| |
public
static Set<String>
killAllBackgroundProcesses() { |
| |
ActivityManager am
= (ActivityManager)
Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE); |
| |
List<ActivityManager.RunningAppProcessInfo>
infos = am.getRunningAppProcesses(); |
| |
Set<String> set
=
new HashSet<>(); |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
for (String pkg
: info.pkgList) { |
| |
am.killBackgroundProcesses(pkg); |
| |
set.add(pkg); |
| |
} |
| |
} |
| |
infos = am.getRunningAppProcesses(); |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
for (String pkg
: info.pkgList) { |
| |
set.remove(pkg); |
| |
} |
| |
} |
| |
return set; |
| |
} |
| |
|
| |
/** |
| |
* 杀死后台服务进程 |
| |
* <p>需添加权限 {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>}</p> |
| |
* |
| |
* @param packageName 包名 |
| |
* @return {@code true}: 杀死成功<br>{@code false}: 杀死失败 |
| |
*/ |
| |
public
static boolean
killBackgroundProcesses(@NonNull
String
packageName) { |
| |
ActivityManager am
= (ActivityManager)
Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE); |
| |
List<ActivityManager.RunningAppProcessInfo>
infos = am.getRunningAppProcesses(); |
| |
if (infos
== null
|| infos.size()
== 0)
return true; |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
if (Arrays.asList(info.pkgList).contains(packageName))
{ |
| |
am.killBackgroundProcesses(packageName); |
| |
} |
| |
} |
| |
infos = am.getRunningAppProcesses(); |
| |
if (infos
== null
|| infos.size()
== 0)
return true; |
| |
for (ActivityManager.RunningAppProcessInfo
info : infos) { |
| |
if (Arrays.asList(info.pkgList).contains(packageName))
{ |
| |
return
false; |
| |
} |
| |
} |
| |
return
true; |
| |
} |
| |
} |