静态广播中实现“回调”

1.我们在广播里,要把数据传递到Activity中时,我们一般使用接口回调,那么动态广播,我们在Activity中去new Broadcast 时,我们可以注册回调接口,可是在静态广播中,是系统去new 出的实例,我们如何使用呢,请看下面代码。
注意:可能引起内存泄漏

保存在Map集合中

ackage com.example.testphone;

import java.util.HashMap;
import java.util.Map;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneReceiverStates extends BroadcastReceiver{

	private String TAG = "PhoneReceiverStates";
    IPhoneReceiverStates mPhoneReceiverStates;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        //去电状态
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Log.e(TAG, "call OUT:" + phoneNumber);
            //刷新界面,为去电状态
            Calling();
        } else {
            //查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电.
            //如果我们想要监听电话的拨打状况,需要这么几步 :
            Log.e(TAG, "来电======================================");
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        }
    }

    PhoneStateListener listener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            //注意,方法必须写在super方法后面,否则incomingNumber无法获取到值。
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.e(TAG, "挂断");
                    EndCallPhone();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.e(TAG, "接听");
                    AnswerThePhone();
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.e(TAG, "响铃:来电号码" + incomingNumber);
                    //输出来电号码
                    PhoneBellring();
                    break;

            }

        }
    };

    public void setPhoneReceiverStatesListener(IPhoneReceiverStates mPhoneReceiverStates) {
        this.mPhoneReceiverStates = mPhoneReceiverStates;
    }

    public interface IPhoneReceiverStates {
        /**
         * 去电中
         */
        void onCalling();

        /**
         * 挂断电话
         */
        void onEndCallPhone();

        /**
         * 接听电话
         */
        void onAnswerThePhone();

        /**
         * 响铃电话
         */
        void onPhoneBellring();
    }

    protected static Map mMapNotifys = new HashMap();

    public static void registerNotify(String notifyKey, IPhoneReceiverStates mIPhoneReceiverStates) {
        if (!mMapNotifys.containsKey(notifyKey)) {
            mMapNotifys.remove(notifyKey);
            mMapNotifys.put(notifyKey, mIPhoneReceiverStates);
        }
    }

    public static void removeNotify(String notifyKey) {
        if (mMapNotifys.containsKey(notifyKey)) {
            mMapNotifys.remove(notifyKey);
        }
    }

    //去电中
    public void Calling() {
        try {
            for (IPhoneReceiverStates notify : mMapNotifys.values()) {
                notify.onCalling();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //挂断电话
    public void EndCallPhone() {
        try {
            for (IPhoneReceiverStates notify : mMapNotifys.values()) {
                notify.onEndCallPhone();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //接听电话
    public void AnswerThePhone() {
        try {
            for (IPhoneReceiverStates notify : mMapNotifys.values()) {
                notify.onAnswerThePhone();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 响铃电话
    public void PhoneBellring() {
        try {
            for (IPhoneReceiverStates notify : mMapNotifys.values()) {
                notify.onPhoneBellring();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
package com.example.testphone;

import java.lang.reflect.Method;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;


public class MainActivity extends Activity implements PhoneReceiverStates.IPhoneReceiverStates {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
			
		//注册电话监听接口
        PhoneReceiverStates.registerNotify("MainActivity",this);
	}

	@Override
	public void onCalling() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onEndCallPhone() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onAnswerThePhone() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPhoneBellring() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		PhoneReceiverStates.removeNotify("MainActivity");
	}
   
   
}

你可能感兴趣的:(静态广播中实现“回调”)