PhoneBroadcast.java 2.04 KB
package com.lotus.town.broadcast;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.controller.NotifyType;
import com.lotus.town.ali.MonitorService;
import com.lotus.town.notify.NotifyManager;
import com.sdk.Debug;
import com.sdk.EventBus;

public class PhoneBroadcast extends BroadcastReceiver {

    private TelephonyManager telMgr;

    @Override
    public void onReceive(Context context, Intent intent) {
        telMgr = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
        switch (telMgr.getCallState()) {
            //来电
            case TelephonyManager.CALL_STATE_RINGING:
                NotifyManager.PhoneStartTime = System.currentTimeMillis();
                NotifyManager.isPhoneStatus = true;
                EventBus.getInstance().notify(EventBus.PHONE_COMING);
                String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                MonitorService.invoke(context);
                break;
            //响铃
            case TelephonyManager.CALL_STATE_OFFHOOK:
                NotifyManager.isPhoneStatus = true;
                NotifyManager.PhoneStartTime = System.currentTimeMillis();
                EventBus.getInstance().notify(EventBus.PHONE_COMING);
                if(Debug.DEBUG_MODE) {
                    Toast.makeText(context, "响铃", Toast.LENGTH_LONG).show();
                }
                break;
            //挂断
            case TelephonyManager.CALL_STATE_IDLE:
                NotifyManager.isPhoneStatus = false;
                NotifyManager.PhoneEndTime = System.currentTimeMillis();
                if(Debug.DEBUG_MODE) {
                    Toast.makeText(context, "挂电话", Toast.LENGTH_LONG).show();
                }
                NotifyManager.NotifyChange(context, NotifyType.PHONE_OVER_NOTIRY);
                MonitorService.invoke(context);
                break;
        }
    }
}