package com.quyunshuo.module.home.utils;

import android.app.Activity;
import android.telephony.TelephonyManager;

import com.zhanshow.mylibrary.phonestate.MyPhoneStateListener;

/**
 * 没有内存泄漏的版本。
 */
public class PhoneStateUtils {
    private static MyPhoneStateListener myPhoneStateListener;

    public PhoneStateUtils() {
    }

    public static void registerPhoneStateListener(Activity activity, MyPhoneStateListener.MyPhoneStateListenerListener listener) {
        myPhoneStateListener = new MyPhoneStateListener();
        myPhoneStateListener.addListener(listener);
        TelephonyManager telephonyManager = (TelephonyManager)activity.getSystemService("phone");
        telephonyManager.listen(myPhoneStateListener, 256);
    }

    public static void unRegisterPhoneStateListener(Activity activity) {
        TelephonyManager telephonyManager = (TelephonyManager)activity.getSystemService("phone");
        telephonyManager.listen(myPhoneStateListener, 0);
        myPhoneStateListener.removeListeners();
        myPhoneStateListener = null;
    }
}