静态广播中实现“回调”

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<String, IPhoneReceiverStates> mMapNotifys = new HashMap<String, IPhoneReceiverStates>();

    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");
	}
   
   
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FW_G8Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值