/* * Copyright (C) 2013 www.418log.org * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ab.util; import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Calendar; 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.location.LocationManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.telephony.TelephonyManager; import android.util.Log; import com.ab.global.AbAppData; // TODO: Auto-generated Javadoc /** * The Class AbAppUtil. */ public class AbAppUtil { /** * 描述:打开并安装文件. * * @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; } } 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; } /** * 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) return files.length; } catch(Exception e) { //Default to return 1 core return 1; } } /** * 描述:判断网络是否有效. * * @param context the context * @return true, if is network available */ public static boolean isNetworkAvailable(Context context) { try { ConnectivityManager connectivity = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo info = connectivity.getActiveNetworkInfo(); if (info != null && info.isConnected()) { if (info.getState() == NetworkInfo.State.CONNECTED) { return true; } } } } catch (Exception e) { return false; } return false; } /** * Gps是否打开 * 需要<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); return lm.isProviderEnabled(LocationManager.GPS_PROVIDER); } /** * wifi是否打开. * * @param context the context * @return true, if is wifi enabled */ public static boolean isWifiEnabled(Context context) { ConnectivityManager mgrConn = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); TelephonyManager mgrTel = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return ((mgrConn.getActiveNetworkInfo() != null && mgrConn .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS); } /** * 判断当前网络是否是wifi网络. * * @param context the context * @return boolean */ public static boolean isWifi(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) { return true; } return false; } /** * 判断当前网络是否是3G网络. * * @param context the context * @return boolean */ public static boolean is3G(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) { return true; } return false; } /** * 描述:设置为调试模式. * * @param debug the new debug */ public static void setDebug(boolean debug){ AbAppData.DEBUG = debug; } /** * 描述:记录当前时间毫秒 * @throws */ public static void prepareStartTime() { Calendar current = Calendar.getInstance(); AbAppData.startLogTimeInMillis = current.getTimeInMillis(); } /** * * 描述:打印这次的执行时间毫秒,需要首先调用logStartTime() * @param D 日志开关标记 * @param tag 标记 * @param msg 描述 */ public static void logEndTime(boolean D,String tag, String msg) { Calendar current = Calendar.getInstance(); long endLogTimeInMillis = current.getTimeInMillis(); if(D) Log.d(tag,msg+":"+(endLogTimeInMillis-AbAppData.startLogTimeInMillis)+"ms"); } /** * 导入数据库 * @param context * @param dbfile * @return */ 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; } }