package com.martin.simpledevelop.utils.app; import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.drawable.Drawable; import android.location.LocationManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import com.martin.simpledevelop.utils.log.SaLogUtils; /** * @Description APP 工具类<br> * @File SaAppUtils.java * @Package com.martin.simpledevelop.utils.app * @Date 2015年6月25日下午11:30:37 * @Author Donghongyu 1358506549@qq.com * @Version v1.0.0 */ public class SaAppUtils { /** * Log 输出标签 */ public static String TAG = SaAppUtils.class.getName(); /** * 获得APP的label名字 * * @param context * @return */ public static String getAppName(Context context) { if (context == null) { SaLogUtils.e(TAG, "getAppName context为空"); return null; } try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); int labelRes = packageInfo.applicationInfo.labelRes; String appName = context.getResources().getString(labelRes); SaLogUtils.i(TAG, "APP名字:\r\n" + appName); return appName; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 获取应用程序版本名称信息 * * @param context * @return */ public static String getVersionName(Context context) { if (context == null) { SaLogUtils.e(TAG, "getVersionName context为空"); return null; } try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); String versionCode = packageInfo.versionName; SaLogUtils.i(TAG, "APP版本名称:\r\n" + versionCode); return packageInfo.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 获取应用程序版本号信息,如果返回0那么说明异常 * * @param context * @return */ public static int getVersionCode(Context context) { if (context == null) { SaLogUtils.e(TAG, "getVersionName context为空"); return 0; } try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); int versionCode = packageInfo.versionCode; SaLogUtils.i(TAG, "APP版本号:\r\n" + versionCode); return packageInfo.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } return 0; } /** * 获取包信息. * * @param context the context */ public static PackageInfo getPackageInfo(Context context) { PackageInfo info = null; try { String packageName = context.getPackageName(); info = context.getPackageManager().getPackageInfo(packageName, 0); SaLogUtils.i(TAG, "APP包信息:\r\n包名>>" + info.packageName + "\r\n第一次安装时间:" + info.firstInstallTime + "\r\n最后一次更新时间:" + info.lastUpdateTime); } catch (Exception e) { e.printStackTrace(); } return info; } /** * 获取应用程序包名 * * @param context * @return */ public static String getPackageName(Context context) { if (context == null) { SaLogUtils.e(TAG, "getPackageName context为空"); return null; } String pkgName = context.getPackageName(); SaLogUtils.i(TAG, "APP包名:\r\n" + pkgName); return pkgName; } /** * 获取应用的图标Drawable对象 * * @param context * @return */ public static Drawable getIcon(Context context) { if (context == null) { SaLogUtils.e(TAG, "getPackageName context为空"); return null; } try { PackageManager packageManager = context.getPackageManager(); PackageInfo packageInfo = packageManager.getPackageInfo( context.getPackageName(), 0); int icon = packageInfo.applicationInfo.icon; Drawable drawable = context.getResources().getDrawable(icon); SaLogUtils.i(TAG, "App 图标:\r\n" + drawable.toString()); return drawable; } catch (NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 描述:打开并安装文件. * * @param context the context * @param file apk文件路径 */ public static void installApk(Context context, File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); context.startActivity(intent); } /** * 描述:卸载程序. * * @param context the context * @param packageName 包名 */ public static void uninstallApk(Context context, String packageName) { Intent intent = new Intent(Intent.ACTION_DELETE); Uri packageURI = Uri.parse("package:" + packageName); intent.setData(packageURI); context.startActivity(intent); } /** * 用来判断服务是否运行. * * @param ctx the ctx * @param className 判断的服务名字 "com.xxx.xx..XXXService" * @return true 在运行 false 不在运行 */ public static boolean isServiceRunning(Context ctx, String className) { boolean isRunning = false; ActivityManager activityManager = (ActivityManager) ctx .getSystemService(Context.ACTIVITY_SERVICE); List<RunningServiceInfo> servicesList = activityManager .getRunningServices(Integer.MAX_VALUE); Iterator<RunningServiceInfo> l = servicesList.iterator(); while (l.hasNext()) { RunningServiceInfo si = (RunningServiceInfo) l.next(); if (className.equals(si.service.getClassName())) { isRunning = true; } } SaLogUtils.i(TAG, className + "运行状态:\r\n" + isRunning); return isRunning; } /** * 停止服务. * * @param ctx the ctx * @param className the class name * @return true, if successful */ public static boolean stopRunningService(Context ctx, String className) { Intent intent_service = null; boolean ret = false; try { intent_service = new Intent(ctx, Class.forName(className)); } catch (Exception e) { e.printStackTrace(); } if (intent_service != null) { ret = ctx.stopService(intent_service); } return ret; } /** * 获取手机的核心梳数量<br> * Gets the number of cores available in this device, across all processors. * Requires: Ability to peruse the filesystem at "/sys/devices/system/cpu" * * @return The number of cores, or 1 if failed to get result */ public static int getNumCores() { try { // Get directory containing CPU info File dir = new File("/sys/devices/system/cpu/"); // Filter to only list the devices we care about File[] files = dir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { // Check if filename is "cpu", followed by a single digit // number if (Pattern.matches("cpu[0-9]", pathname.getName())) { return true; } return false; } }); // Return the number of cores (virtual CPU devices) SaLogUtils.i(TAG, "手机的CPU核心数量:" + files.length); return files.length; } catch (Exception e) { // Default to return 1 core return 1; } } /** * Gps是否打开 需要<br> * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" * />权限 * * @param context the context * @return true, if is gps enabled */ public static boolean isGpsEnabled(Context context) { LocationManager lm = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); // GPS的开启状态 boolean isGpsEnablees = lm .isProviderEnabled(LocationManager.GPS_PROVIDER); SaLogUtils.i(TAG, "GPS开启状态:\r\n" + isGpsEnablees); return isGpsEnablees; } /** * 判断当前网络是否是移动数据网络. * * @param context the context * @return boolean */ public static boolean isMobile(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) { SaLogUtils.i(TAG, "当前手机是否是连接移动网络:\r\n true"); return true; } SaLogUtils.i(TAG, "当前手机是否是连接移动网络:\r\n false"); return false; } /** * 导入数据库. * * @param context the context * @param dbName the db name * @param rawRes the raw res * @return true, if successful */ public static boolean importDatabase(Context context, String dbName, int rawRes) { int buffer_size = 1024; InputStream is = null; FileOutputStream fos = null; boolean flag = false; try { String dbPath = "/data/data/" + context.getPackageName() + "/databases/" + dbName; File dbfile = new File(dbPath); // 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 if (!dbfile.exists()) { // 欲导入的数据库 if (!dbfile.getParentFile().exists()) { dbfile.getParentFile().mkdirs(); } dbfile.createNewFile(); is = context.getResources().openRawResource(rawRes); fos = new FileOutputStream(dbfile); byte[] buffer = new byte[buffer_size]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.flush(); } flag = true; } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (Exception e) { } } if (is != null) { try { is.close(); } catch (Exception e) { } } } return flag; } }