判断app是否在界面显示(焦点)
public static boolean isAppInForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
return appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
}
}
return false;
}
判断当前语言环境是否为中文
public static boolean isZh(Context context) {
Locale locale = context.getResources().getConfiguration().locale;
String language = locale.getLanguage();
if (language.endsWith("zh"))
return true;
else
return false;
}
判断当前网络是否可用
public static boolean isNetwork(Activity activity) {
Context context = activity.getApplicationContext();
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
} else {
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0) {
for (int i = 0; i < networkInfo.length; i++) {
System.out.println(i + "===状态==="
+ networkInfo[i].getState());
System.out.println(i + "===类型==="
+ networkInfo[i].getTypeName());
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
判断网络状态是移动网络还是wifi网络
public static String getNetworkType(Context mContext) {
String netType = "";
ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
netType = "NETTYPE_CMNET";
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = "NETTYPE_WIFI";
}
return netType;
}
判断手机中是否安装指定包名的软件
public static boolean isInstallByread(Context context, String name) {
List<PackageInfo> packages = context.getPackageManager()
.getInstalledPackages(0);
for (int i = 0; i < packages.size(); i++) {
PackageInfo packageInfo = packages.get(i);
if (packageInfo.packageName.equals(name)) {
return true;
} else {
continue;
}
}
return false;
}
判断当前情景模式(如静音模式,震动模式)
public static String isSceneMode(Context mContext){
AudioManager audio = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
String type = "正常模式";
if (audio.getRingerMode()==AudioManager.RINGER_MODE_SILENT) {
type = "静音模式";
}else if(audio.getRingerMode()==
AudioManager.RINGER_MODE_VIBRATE){
type = "震动模式";
}
return type;
}