package com.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;

public class WakeLockUtil {
	private static WifiLock wifiLock;

	@SuppressLint("InvalidWakeLockTag")
	public static void acquireWakeLock(Activity activity, WakeLock wakeLock) {
		if (wakeLock == null) {
			PowerManager pm = (PowerManager) activity
					.getSystemService(Context.POWER_SERVICE);
			wakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK
					| PowerManager.ACQUIRE_CAUSES_WAKEUP, "crtech");
		}
		if (wifiLock == null) {
			WifiManager manager = (WifiManager) activity.getApplicationContext()
					.getSystemService(Context.WIFI_SERVICE);
			wifiLock = manager.createWifiLock("SwiFTP");
			wifiLock.setReferenceCounted(false);
		}
		if (wakeLock != null) {
			System.out.println(wakeLock+"------------------锁定");
			wakeLock.acquire();
			System.out.println(wakeLock+"----------==--------锁定");
		}
		if (wifiLock != null) {
			System.out.println("------------------wifi锁定");
			wifiLock.acquire();
		}
	}

	// 释放设备电源锁
	public static void releaseWakeLock(WakeLock wakeLock) {
		System.out.println("------------------解除锁定=====================");
		if (wakeLock != null) {
			wakeLock.release();
			System.out.println("------------------解除锁定");
			wakeLock = null;
		}
		if (wifiLock != null) {
			wifiLock.release();
			wifiLock = null;
		}
	}

}
