Android开发中经常会用到的功能代码

/**
	 * 根据id读取Raw目录文件
	 * @param context
	 * @param resId
	 * @return
	 */
	public static String geFileFromRaw(Context context, int resId) {
		StringBuilder s = new StringBuilder();
		try {
			InputStreamReader in = new InputStreamReader(context.getResources()
					.openRawResource(resId));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

/**
	 * 根据文件名读取Assets目录下的文件
	 * @param context
	 * @param fileName
	 * @return String
	 */
	public static String geFileFromAssets(Context context, String fileName) {
		StringBuilder s = new StringBuilder("");
		try {
			InputStreamReader in = new InputStreamReader(context.getResources()
					.getAssets().open(fileName));
			BufferedReader br = new BufferedReader(in);
			String line;
			while ((line = br.readLine()) != null) {
				s.append(line);
			}
			return s.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	/***
	 * 屏幕截图
	 */
	public class ScreenShot {
		// 获取指定Activity的截屏,保存到png文件
		public static Bitmap takeScreenShot(Activity activity) {
			// 截图的View
			View view = activity.getWindow().getDecorView();
			view.setDrawingCacheEnabled(true);
			view.buildDrawingCache();
			Bitmap b1 = view.getDrawingCache();
			// 获取状态栏高度
			Rect frame = new Rect();
			activity.getWindow().getDecorView()
					.getWindowVisibleDisplayFrame(frame);
			int statusBarHeight = frame.top;
			// 获取屏幕长和高
			int width = activity.getWindowManager().getDefaultDisplay()
					.getWidth();
			int height = activity.getWindowManager().getDefaultDisplay()
					.getHeight();
			// 去掉标题栏
			Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width,
					height - statusBarHeight);
			view.destroyDrawingCache();
			return b;
		}

		// 保存到sdcard
		public static void savePic(Bitmap b, String strFileName) {
			OutputStream fos = null;
			File imagFile = new File(strFileName);
			try {
				imagFile.createNewFile();
				fos = new FileOutputStream(imagFile);
				if (null != fos) {
					b.compress(Bitmap.CompressFormat.PNG, 90, fos);
					fos.flush();
					fos.close();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

/**
 * json字符串转换成实体类
 * @author Administrator
 *
 */
public class JsonUtil {
	private static Gson gson = new Gson();

	public static <T> T fromJson(String json, Class<T> classOfT) {
		return gson.fromJson(json, classOfT);
	}
}

/**
	 * Bitmap 缩放
	 * 
	 * @param bmp
	 * @param scale
	 * @return
	 */
	private Bitmap resizeBitmap(Bitmap bmp, float scale) {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		Matrix matrix = new Matrix();
		matrix.postScale(scale, scale);
		Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
				matrix, true);
		return resizeBmp;
	}

/***
	 * 判断apk是否安装
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean isApkInstalled(Context context, String packageName) {
		PackageManager pm = context.getPackageManager();
		boolean installed = false;
		try {
			pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
			installed = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return installed;
	}

   
/**
	 * 安装Apk
	 * 
	 * @param context
	 * @param apkPath
	 */
	public static void installApk(Context context, String apkPath) {

		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setDataAndType(Uri.parse("file://" + apkPath),
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}

/**
	 * 得到已安装Apk文件的源Apk文件路径,例如(/data/app/xxx.apk)
	 * 
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static String getSourceApkPath(Context context, String packageName) {
		if (TextUtils.isEmpty(packageName))
			return null;
		try {
			ApplicationInfo appInfo = context.getPackageManager()
					.getApplicationInfo(packageName, 0);
			return appInfo.sourceDir;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}

// 不同单位之间的转换工具类
	public class DisplayUtils {

		public static float dp2px(Resources resources, float dp) {
			final float scale = resources.getDisplayMetrics().density;
			return dp * scale + 0.5f;
		}

		/**
		 * sp转px
		 * 
		 * @param context
		 * @param dipValue
		 * @return
		 */
		public static float sp2px(Resources resources, float sp) {
			final float scale = resources.getDisplayMetrics().scaledDensity;
			return sp * scale;
		}

		/**
		 * dip转px
		 * 
		 * @param context
		 * @param dipValue
		 * @return
		 */
		public static int dip2px(Context context, float dipValue) {

			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (dipValue * scale + 0.5f);

		}

		/**
		 * px转dip
		 * 
		 * @param context
		 * @param pxValue
		 * @return
		 */
		public static int px2dip(Context context, float pxValue) {

			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (pxValue / scale + 0.5f);

		}
	}

/**
	 * SD卡操作
	 * 
	 * @author
	 * 
	 */
	public class SDCardHelper {

		private static SDCardHelper mSDCardHelper = new SDCardHelper();

		private SDCardHelper() {

		}

		public static SDCardHelper getInstance() {
			return mSDCardHelper;
		}

		/**
		 * 获得sd卡的可用空间
		 * 
		 * @return
		 */
		public long getAvailableSdCardSize() {
			String path = Environment.getExternalStorageDirectory().getPath();
			StatFs statFs = new StatFs(path);
			long blockSize = statFs.getBlockSize();
			int available = statFs.getAvailableBlocks();
			return available * blockSize;
		}

		/**
		 * 检查是否安装了sd卡
		 * 
		 * @return
		 */
		public boolean sdCardMounted() {
			final String state = Environment.getExternalStorageState();
			if (state.equals(Environment.MEDIA_MOUNTED)
					&& !state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
				return true;
			}
			return false;
		}

	}

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkCheck {
	
	/**
	 * 判断网络是否连接
	 * @param context
	 * @return
	 */
	public boolean isConnection(Context context) {

		ConnectivityManager connectMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mobNetInfo = connectMgr
				.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		NetworkInfo wifiNetInfo = connectMgr
				.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
			System.out.println("网络连接状态======false");
			return false;

		} else {
			System.out.println("网络连接状态======true");

			return true;
		}

	}
	
	/**
	 * 判断网络是否连接
	 * @param context
	 * @return
	 */
	public boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager
					.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

	/**
	 * 判断WIFI网络是否可用 
	 * @param context
	 * @return
	 */
	public boolean isWifiConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mWiFiNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			if (mWiFiNetworkInfo != null) {
				return mWiFiNetworkInfo.isAvailable();
			}
		}
		return false;
	}
	
	/**
	 * 判断手机网络是否可用 
	 * @param context
	 * @return
	 */
	public boolean isMobileConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mMobileNetworkInfo = mConnectivityManager
					.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
			if (mMobileNetworkInfo != null) {
				return mMobileNetworkInfo.isAvailable();
			}
		}
		return false;
	}

}


其他常用代码片段逐渐收集中


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值