50 lines
1.4 KiB
Java
50 lines
1.4 KiB
Java
package com.mogo.utils;
|
|
|
|
import android.app.ActivityManager;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
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;
|
|
}
|
|
}
|
|
}
|