Android 监听去电接通状态,注意不是来电

又是一年回家时,订票难呀,难于上青天。

闲来无事,想写一个电话订票的软件,电话不通断线后自动重拔,在网上找了一些资源,发现对于来电状态还好,去电状态资源很少,无奈去看下Phone源码,总算找到一个方法,但是有局限性,即必须在源码下编译才行,并且要和系统签名一致才可使用。下面是具体代码

1.PhoneApp类通过CallManager去注册一个电话状态

public class PhoneApp extends Application{
public CallManager mCM;
private static PhoneApp isMe;
 public PhoneApp(){
       isMe=this;
 }
 static  PhoneApp getInstance(){
  return isMe;
 }
 @Override
  public void onCreate(){
     mCM = CallManager.getInstance();
     Phone  phone = PhoneFactory.getDefaultPhone();
     mCM.registerPhone(phone);

    }
 

}

2.Service去监听去电状态

public class SpeedService extends Service {
	private TelephonyManager telephonyManager;
	private String PhoneNumber;
	private int dialCount;
        private int hasdialCount;
	private Intent callIntent;
	private CallManager mCM;
        private boolean hasConnect=false;
        private boolean isUseCount=false;
	private static final int PHONE_STATE_CHANGED=102;
       private Vibrator mVibrate;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case PHONE_STATE_CHANGED:
				updatePhoneSateChange();
				break;

			default:
				break;
			}
			
			
		};
		
	};
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
           mVibrate=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
           telephonyManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
          mCM=PhoneApp.getInstance().mCM;
          mCM.registerForPreciseCallStateChanged(mHandler, PHONE_STATE_CHANGED, null); 
  
	}
	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub
		super.onStart(intent, startId);
                 hasConnect=false;
                 isUseCount=false;
                 hasdialCount=0;
		PhoneNumber=intent.getStringExtra("phone");
		dialCount=Integer.parseInt(intent.getStringExtra("count"));
                 if(dialCount!=0){
                    isUseCount=true;
                 }
		callIntent=new Intent(Intent.ACTION_CALL);
		callIntent.setData(Uri.parse("tel:"+PhoneNumber));
		callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		call();
	 }
	
	private void call(){
            if(isUseCount&&hasdialCount<=dialCount){
                startActivity(callIntent);	
                hasdialCount++;
             }else if(!isUseCount){
               startActivity(callIntent);
             }
	   
	}
 private void updatePhoneSateChange(){
		Call fgCall = mCM.getActiveFgCall();
		if (mCM.hasActiveRingingCall()) {
	            fgCall = mCM.getFirstActiveRingingCall();
	      }

        final Call.State state = fgCall.getState();
        switch (state) {
            case IDLE:
             if(!hasConnect){
                    call();
                }
                break;

            case ACTIVE://去电接通
               mVibrate.vibrate(1500);
            	hasConnect=true;
            	break;
            default:
                break;
        }
		
	}
    	
    };

   

}

下载地址:http://download.csdn.net/detail/androidchuxueze/5005416

代码必须在源码下编译,如果没手机系统签名,将无法安装




你可能感兴趣的:(Android 监听去电接通状态,注意不是来电)