android 调用拨号返回调用界面
(2012-06-20 15:13:12)
调用拨号界面,即使用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) {
}