日拱一卒(五十五)

1.android:exported:
android:exported =false;Service可以是否被其他应用访问;

2.Android中WakeLock使用功能:
PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PostLocationService");  

wakeLock.acquire();----获取锁  
wakeLock.release();-----释放锁  


权限声明:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DEVICE_POWER"/>

pm.newWakeLock()中第一个参数可取下列值:
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
ACQUIRE_CAUSES_WAKEUP:强制使屏幕亮起,这种锁主要针对一些必须通知用户的操作.
ON_AFTER_RELEASE:当锁被释放时,保持屏幕亮起一段时间

3.byte,long,double,int,之间互转

/**
	 * int转byte[],四个字节
	 * @param value
	 * @return
	 */
	public static byte[] intToBytes(int value) {
		byte[] src = new byte[4];
		if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
			for (int i = 0; i < src.length; i++) {
				src[i] = (byte) ((value >> (8 * i)) & 0xFF);
			}
		} else {
			for (int i = 0; i < src.length; i++) {
				src[i] = (byte) ((value >> (8 * (src.length - 1 - i))) & 0xFF);
			}
		}
		return src;
	}

	/**
	 * byte[]转int
	 * @param src
	 * @return
	 */
	public static int bytesToInt(byte[] src) {
		return bytesToInt(src, 0, 4);
	}

	/**
	 * byte[]中指定位置转int
	 * @param src
	 * @param start 起始位置,含
	 * @param end 模式位置,不含
	 * @return
	 */
	public static int bytesToInt(byte[] src, int start, int end) {
		int retVal = 0;
		if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
			for (int i = start; i < end; i++) {
				retVal |= (((src[i] & 0xFF) << (8 * i)));
			}
		} else {
			for (int i = start; i < end; i++) {
				retVal |= (((src[i] & 0xFF) << (8 * (end - 1 - i))));
			}
		}
		return retVal;
	}

	/**
	 * long转byte[]
	 * @param value
	 * @return
	 */
	public static byte[] longToBytes(long value) {
		byte[] src = new byte[8];
		if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
			for (int i = 0; i < src.length; i++) {
				src[i] = (byte) ((value >> (8 * i)) & 0xFF);
			}
		} else {
			for (int i = 0; i < src.length; i++) {
				src[i] = (byte) ((value >> (8 * (src.length - 1 - i))) & 0xFF);
			}
		}
		return src;
	}

	/**
	 * byte[]中指定位置转long
	 * @param src
	 * @param start 起始位置,含
	 * @param end 模式位置,不含
	 * @return
	 */
	public static long bytesToLong(byte[] src, int start, int end) {
		long retVal = 0;
		if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
			for (int i = start; i < end; i++) {
				retVal |= (((src[i] & 0xFF) << (8 * i)));
			}
		} else {
			for (int i = start; i < end; i++) {
				retVal |= (((src[i] & 0xFF) << (8 * (end - 1 - i))));
			}
		}
		return retVal;
	}

	/**
	 * byte[]转long
	 * @param src
	 * @return
	 */
	public static long bytesToLong(byte[] src) {
		return bytesToLong(src, 0, 8);
	}

	/**
	 * 将几个byte[]组合成新的byte[]
	 * @param arrs
	 * @return
	 */
	public static byte[] addByteArr(byte[]... arrs) {
		int len = 0;
		for (byte[] byteOnes : arrs) {
			len += byteOnes.length;
		}
		byte[] retVal = new byte[len];
		int offset = 0;
		for (byte[] byteTwos : arrs) {
			System.arraycopy(byteTwos, 0, retVal, offset, byteTwos.length);
			offset += byteTwos.length;
		}
		return retVal;
	}

	/**
	 * byte[]转double
	 * @param bytes
	 * @return
	 */
	public static double getDouble(byte[] bytes) {
		long l = getLong(bytes);
		return Double.longBitsToDouble(l);
	}

	/**
	 * double转byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getBytes(double data) {
		long intBits = Double.doubleToLongBits(data);
		return getBytes(intBits);
	}

	/**
	 * 将bytes[]转成long
	 * @param bytes
	 * @return
	 */
	public static long getLong(byte[] bytes) {
		return (0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8))
				| (0xff0000L & ((long) bytes[2] << 16)) | (0xff000000L & ((long) bytes[3] << 24))
				| (0xff00000000L & ((long) bytes[4] << 32))
				| (0xff0000000000L & ((long) bytes[5] << 40))
				| (0xff000000000000L & ((long) bytes[6] << 48))
				| (0xff00000000000000L & ((long) bytes[7] << 56));
	}

	/**
	 * long转byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getBytes(long data) {
		byte[] bytes = new byte[8];
		bytes[0] = (byte) (data & 0xff);
		bytes[1] = (byte) ((data >> 8) & 0xff);
		bytes[2] = (byte) ((data >> 16) & 0xff);
		bytes[3] = (byte) ((data >> 24) & 0xff);
		bytes[4] = (byte) ((data >> 32) & 0xff);
		bytes[5] = (byte) ((data >> 40) & 0xff);
		bytes[6] = (byte) ((data >> 48) & 0xff);
		bytes[7] = (byte) ((data >> 56) & 0xff);
		return bytes;
	}

4.打印byte[]

/**
	 * Encodes an array of bytes as String representation of hexadecimal.
	 * @param bytes an array of bytes to convert to a hex string.
	 * @return generated hex string.
	 */
	public static String encodeHex(byte[] bytes) {
		StringBuilder hex = new StringBuilder(bytes.length * 2);
		for (byte aByte : bytes) {
			if ((aByte & 0xff) < 0x10) {
				hex.append("0");
			}
			hex.append(Integer.toString(aByte & 0xff, 16));
		}
		return hex.toString();
	}





你可能感兴趣的:(android)