Android中常用开发工具类—持续更新...

一、自定义ActionBar

public class ActionBarTool {
	public static void setActionBarLayout(Activity act,Context context,int layoutId){//前两个参数一般情况下都可以填this,第三个参数为自定义View的Id
	    ActionBar actionBar = act.getActionBar();
	    if( null != actionBar ){
	        actionBar.setDisplayShowHomeEnabled(false);//是否显示左上角的应用图标
	        actionBar.setDisplayShowCustomEnabled(true);//是否使用自定义标题栏
	        LayoutInflater inflator = (LayoutInflater)   context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        View v = inflator.inflate(layoutId, null);//填充自定义布局
	        ActionBar.LayoutParams layout = new ActionBar.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
	        actionBar.setCustomView(v,layout);//把自定义View设置成ActionBar
	    }
	}
}

二、系统信息工具类

public class SystemInfoUtils {
	public static int getRunningAppProcesses(Context context) {// 得到当前系统内运行的app进程数量
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appInfos = am.getRunningAppProcesses();
		return appInfos.size();
	}

	public static List<HashMap<String, Object>> getAppInfo(Context context) {// 得到非系统应用的app信息
		List<HashMap<String, Object>> appInfos = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map = null;
		List<PackageInfo> packages = context.getPackageManager()
				.getInstalledPackages(0);// 参数传入0表示过滤权限,因为可能有的手机不让你获取应用信息
		for (PackageInfo temp : packages) {
			if ((temp.applicationInfo.flags & temp.applicationInfo.FLAG_SYSTEM) == 0) {
				// 非系统应用
				// 得到应用的名称
				String appName = temp.applicationInfo.loadLabel(
						context.getPackageManager()).toString();
				// 得到应用图标
				Drawable appIcon = temp.applicationInfo.loadIcon(context
						.getPackageManager());
				// 得到应用最后一次更新的时间
				long lastUpdateTime = temp.lastUpdateTime;
				// 得到应用的包名
				String packageName = temp.packageName;
				// 得到应用的版本信息
				String versionName = temp.versionName;
				map = new HashMap<String, Object>();
				map.put("appName", appName);
				map.put("appIcon", appIcon);
				map.put("lastUpdateTime", lastUpdateTime);
				map.put("packageName", packageName);
				map.put("versionName", versionName);
				appInfos.add(map);// 把应用信息加入集合中返回
				map = null;
			} else {
				// 系统应用
			}
		}
		return appInfos;
	}

	// 获取sd卡容量
	public static String getSdSize(Context context) {
		String totalStr = null, availStr = null;
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {// 判断是否检测到sd卡
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());

			long blockSize = stat.getBlockSizeLong();
			long availableBlocks = stat.getAvailableBlocksLong();
			long totalBlocks = stat.getBlockCountLong();

			long totalSize = blockSize * totalBlocks;
			long availSize = blockSize * availableBlocks;

			totalStr = Formatter.formatFileSize(context, totalSize);
			availStr = Formatter.formatFileSize(context, availSize);

		} else
			Toast.makeText(context, "没有检测到SD卡,请检查是否正确插入", Toast.LENGTH_SHORT)
					.show();
		return "SD卡总容量为:" + totalStr + "可用为:" + availStr;
	}

	// 获取手机内存
	public static String getRomSave(Context context) {
		String totalStr = null, availStr = null;
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());// 得到手机内存的路径

		long blockSize = stat.getBlockSizeLong();
		long availableBlocks = stat.getAvailableBlocksLong();
		long totalBlocks = stat.getBlockCountLong();

		long totalSize = blockSize * totalBlocks;
		long availSize = blockSize * availableBlocks;

		totalStr = Formatter.formatFileSize(context, totalSize);
		availStr = Formatter.formatFileSize(context, availSize);

		return "手机内存总大小为:" + totalStr + "可用空间为:" + availStr;
	}
}

三、网络相关工具类

public class NetworkUtils {
	// 判断当前网络是否可用
	public static boolean networkIsConnect(Context context) {
		ConnectivityManager conn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conn.getActiveNetworkInfo();
		if (info != null && info.isConnected()) {
			return true;
		} else {
			return false;
		}
	}

	// 判断当前连接的网络是否是wifi,并得到连接当前Wifi的信息
	public static boolean networkIsWifi(Context context) {
		ConnectivityManager conn = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conn.getActiveNetworkInfo();
		if (info != null && info.isConnected()
				&& info.getType() == ConnectivityManager.TYPE_WIFI) {
			WifiManager wifiManager = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			Toast.makeText(context, "连接的wifi网络的id为:"+wifiInfo.getNetworkId(), Toast.LENGTH_SHORT).show();
			return true;
		} else {
			return false;
		}
	}

	// 是否打开Wifi
	public static void setWifiEnabled(Context context, boolean enabled) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		if (enabled) {
			wifiManager.setWifiEnabled(true);
		} else {
			wifiManager.setWifiEnabled(false);
		}
	}

}

四、Px与Dp转化工具类

public class DensityUtils {
	public static int Dp2Px(Context context, float dp) {
	    final float scale = context.getResources().getDisplayMetrics().density;
	    return (int) (dp * scale + 0.5f);
	} 

	public static int Px2Dp(Context context, float px) {
	    final float scale = context.getResources().getDisplayMetrics().density;
	    return (int) (px / scale + 0.5f);
	}
}

五、震动工具类

public class VibratorUtil {  

    /**
     * long milliseconds :震动的时长,单位是毫秒
     * long[] pattern  :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒
     * boolean isRepeat : 是否反复震动,如果是true,反复震动,如果是false,只震动一次
     */
	 public static void Vibrate(final Context context) {
		 long milliseconds = 100;
         Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
         vib.vibrate(milliseconds);
  }
     public static void Vibrate(final Context context, long milliseconds) {
            Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
            vib.vibrate(milliseconds);
     }
     public static void Vibrate(final Context context, long[] pattern,boolean isRepeat) {
            Vibrator vib = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
            vib.vibrate(pattern, isRepeat ? 1 : -1);//-1为反复震动
     }   

}  

——持续更新......



上一篇:C++知识点总结(三)


下一篇:NumPy速查笔记(持续更新中)