125 lines
4.6 KiB
Java
125 lines
4.6 KiB
Java
package com.mogo.utils;
|
||
|
||
import android.app.ActivityManager;
|
||
import android.content.ComponentName;
|
||
import android.content.Context;
|
||
import android.content.pm.ApplicationInfo;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.text.TextUtils;
|
||
|
||
import java.util.List;
|
||
|
||
/**
|
||
* @author congtaowang
|
||
* @since 2020-04-09
|
||
* <p>
|
||
* 描述
|
||
*/
|
||
public class AppUtils {
|
||
|
||
public static boolean isApplicationBroughtToBackground( final Context context ) {
|
||
ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||
List< ActivityManager.RunningTaskInfo > tasks = am.getRunningTasks( 1 );
|
||
if ( !tasks.isEmpty() ) {
|
||
ComponentName topActivity = tasks.get( 0 ).topActivity;
|
||
if ( !topActivity.getPackageName().equals( context.getPackageName() ) ) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public static boolean isAppInstalled( Context context, String pkg ) {
|
||
PackageInfo packageInfo;
|
||
if ( TextUtils.isEmpty( pkg ) ) {
|
||
return false;
|
||
}
|
||
try {
|
||
packageInfo = context.getPackageManager().getPackageInfo( pkg, 0 );
|
||
} catch ( PackageManager.NameNotFoundException e ) {
|
||
packageInfo = null;
|
||
e.printStackTrace();
|
||
}
|
||
if ( packageInfo == null ) {
|
||
return false;
|
||
} else {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public static String getApplicationLabel( Context context, String pkgName ) {
|
||
try {
|
||
PackageManager pm = context.getPackageManager();
|
||
ApplicationInfo appInfo = pm.getApplicationInfo( pkgName, PackageManager.GET_META_DATA );
|
||
return pm.getApplicationLabel( appInfo ).toString();
|
||
} catch ( Exception e ) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
public static boolean isAppForeground( Context context ) {
|
||
if ( context != null ) {
|
||
ActivityManager activityManager = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||
List< ActivityManager.RunningAppProcessInfo > processes = activityManager.getRunningAppProcesses();
|
||
for ( ActivityManager.RunningAppProcessInfo processInfo : processes ) {
|
||
if ( processInfo.processName.equals( context.getPackageName() ) ) {
|
||
if ( processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ) {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public static boolean isAppForeground( Context context, String pkg ) {
|
||
if ( context != null ) {
|
||
ActivityManager activityManager = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||
List< ActivityManager.RunningAppProcessInfo > processes = activityManager.getRunningAppProcesses();
|
||
for ( ActivityManager.RunningAppProcessInfo processInfo : processes ) {
|
||
if ( processInfo.processName.equals( pkg ) ) {
|
||
if ( processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND ) {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
//获取已安装应用的 uid,-1 表示未安装此应用或程序异常
|
||
public static int getPackageUid( Context context, String packageName ) {
|
||
try {
|
||
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo( packageName, 0 );
|
||
if ( applicationInfo != null ) {
|
||
return applicationInfo.uid;
|
||
}
|
||
} catch ( Exception e ) {
|
||
return -1;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
/**
|
||
* 判断某一 uid 的程序是否有正在运行的进程,即是否存活
|
||
* Created by cafeting on 2017/2/4.
|
||
*
|
||
* @param context 上下文
|
||
* @param uid 已安装应用的 uid
|
||
* @return true 表示正在运行,false 表示没有运行
|
||
*/
|
||
public static boolean isProcessRunning( Context context, int uid ) {
|
||
ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||
List< ActivityManager.RunningServiceInfo > runningServiceInfos = am.getRunningServices( 200 );
|
||
if ( runningServiceInfos.size() > 0 ) {
|
||
for ( ActivityManager.RunningServiceInfo appProcess : runningServiceInfos ) {
|
||
if ( uid == appProcess.uid ) {
|
||
return true;
|
||
}
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
}
|