android 调用拨号返回调用界面

android 调用拨号返回调用界面

  (2012-06-20 15:13:12)
标签: 

it

分类: Android

   调用拨号界面,即使用startActivityForResult()也不会返回调用者界面,可变通方法取监听电话状态,在挂断后返回调用者.
   主要是透过
   TelephonyManager.listen(lsnr, PhoneStateListener.LISTEN_CALL_STATE); 注册监听callback
 
   private class PhoneCallListener extends PhoneStateListener {
              private boolean bphonecalling = false;

              @Override
              public void onCallStateChanged(int state, String incomingnumber) {
                     // seems the incoming number is this call back always ""
                     if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                            bphonecalling = true;
                     } else if (TelephonyManager.CALL_STATE_IDLE == state
                                   && bphonecalling) {
                            if (mTtelephonyManager != null) {
                                   mTtelephonyManager.listen(mPhoneCallListener,
                                                 PhoneStateListener.LISTEN_NONE);
                            }
                            bphonecalling = false;
                           
                            Intent i = getPackageManager().getLaunchIntentForPackag e(
                                          getPackageName());
                            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                                          | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                            startActivity(i);
                     }
                     super.onCallStateChanged(state, incomingnumber);
              }
       }
      
       实际中"incomingnumber"一直是"",导致无法准确判读是否主动拨出的号码通话,是个缺陷.
       另外intent flags Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP 的设置
       会把调用者所在task放到前台,此时调用者可能不是最top的,这样可以返回用户最后1次操作的界面,不会太突兀.
       如果需要准确返回调用者,可以设置为Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP
      
另外 附上一段中止通话的方法:
       TelephonyManager telmgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
              try {
                     Class c = Class.forName(telmgr.getClass().getName());
                     Method m = c.getDeclaredMethod("getITelephony");
                     m.setAccessible(true);
                     ITelephony telephonyService = (ITelephony) m.invoke(telmgr);
                     if (telephonyService != null) {
                            if (!telephonyService.isIdle()) {
                                   telephonyService.endCall();
                            }
                     }
              } catch (Exception e) {
              }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值