android > 广播监听电话状态

新建一个广播

 PhoneStatReceiver.java

package talk.mft;

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

public class PhoneStatReceiver extends BroadcastReceiver{
    
    private static final String TAG = "PhoneStatReceiver";


    @Override
    public void onReceive(Context context, Intent intent) {
            //如果是拨打电话
    		//Log.d(TAG, intent.getAction()+"");
    		TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE); 
    		Log.d(TAG, "status:"+tm.getCallState());
    		Log.d(TAG,"tele_state:"+intent.getStringExtra(TelephonyManager.EXTRA_STATE)+"");
    		
    		  
    		
    		/*
            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                        
                    incomingFlag = false;
                    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);        
                    Log.i(TAG, "call OUT:"+phoneNumber);                        
            }else{                         
                    //如果是来电
                    TelephonyManager tm = 
                        (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        
                    
                    switch (tm.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING:
                            incomingFlag = true;//标识当前是来电
                            incoming_number = intent.getStringExtra("incoming_number");
                            Log.i(TAG, "RINGING :"+ incoming_number);
                            break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:                                
                            if(incomingFlag){
                                    Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
                            }
                            break;
                    
                    case TelephonyManager.CALL_STATE_IDLE:                                
                            if(incomingFlag){
                                    Log.i(TAG, "incoming IDLE");                                 
                            }
                            break;
                    } 
            }
            */
    		
    }
}

 

 

在manifest 里注册并 附加权限

     <application>............
		<receiver android:name=".x.x.PhoneStatReceiver">  
			<intent-filter>
				<action android:name="android.intent.action.PHONE_STATE"/>           
				<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
			</intent-filter>
		</receiver>        
    </application>
	<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>  
	<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
	<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
	

 

 

 

 

电话状态监听

public class CallReady extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//----------- phone status listen
		/* 取得电话服务 */
		TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		//监听电话的状态
		telManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
		//=====
	}
	
	//挂断电话
	private void endCall()
	{
		try
		{
			//通过反射拿到android.os.ServiceManager里面的getService这个方法的对象
			Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
			//通过反射调用这个getService方法,然后拿到IBinder对象,然后就可以进行aidl啦
			IBinder iBinder = (IBinder) method.invoke(null, new Object[] {TELEPHONY_SERVICE});
			ITelephony telephony = ITelephony.Stub.asInterface(iBinder);
			telephony.endCall();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	//
	PhoneStateListener listener = new PhoneStateListener(){	
		@Override  public void onCallStateChanged(int state, String incomingNumber) {
		      switch (state){
		        case TelephonyManager.CALL_STATE_IDLE: /* 无任何状态时 */
		        	if(onCreateFlag){
		        		Log.d(TAG,"xxxxxxxxx");//如果挂断
		        	}
		        	break;
		        case TelephonyManager.CALL_STATE_OFFHOOK: /* 接起电话时 */
		        	if(onCreateFlag && curentTime >= 1){
		        		Log.d(TAG,"gggggggggggg");//如果被接起
		        	}
		        	
		        	break;	
		        case TelephonyManager.CALL_STATE_RINGING: /* 电话进来时 */
		        	 //Log.d(TAG,"rrrrrrrrrrrr");
		        	break;
		        default:
			break;
		      }
		super.onCallStateChanged(state, incomingNumber);
		}        	
	};
	//\

}

 

 

 

 

你可能感兴趣的:(android)