// phoneServiceName是服务名,一般是 "phone" --> Context.TELEPHONY_SERVICE
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(phoneServiceName);
if(telephonyManager != null) {
try {
// 注册来电监听
telephonyManager.listen(mTelephonyListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
// 异常捕捉
}
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
// 电话挂断
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// 来电响铃
break;
case TelephonyManager.CALL_STATE_RINGING:
// 来电接通
break;
default:
break;
}
}
/** Device call state: No activity. */ public static final int CALL_STATE_IDLE = 0; // 电话挂断 /** Device call state: Ringing. A new call arrived and is * ringing or waiting. In the latter case, another call is * already active. */ public static final int CALL_STATE_RINGING = 1; // 来电响铃 /** Device call state: Off-hook. At least one call exists * that is dialing, active, or on hold, and no calls are ringing * or waiting. */ public static final int CALL_STATE_OFFHOOK = 2; // 来电接通
// OutgoingCallListener继承一个BroadcastReceiver
<receiver android:name="com.test.OutgoingCallListener" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
public ArrayList<String> getMultSimCardInfo() {
// 获取双卡的信息,这个也是经验尝试出来的,不知道其他厂商有什么坑
ArrayList<String> phoneServerList = new ArrayList<String>();
for(int i = 1; i < 3; i++) {
try {
String phoneServiceName;
if (MiuiUtils.isMiuiV6()) {
phoneServiceName = "phone." + String.valueOf(i-1);
} else {
phoneServiceName = "phone" + String.valueOf(i);
}
// 尝试获取服务看是否能获取到
IBinder iBinder = ServiceManager.getService(phoneServiceName);
if(iBinder == null) continue;
ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
if(iTelephony == null) continue;
phoneServerList.add(phoneServiceName);
} catch(Exception e) {
e.printStackTrace();
}
}
// 这个是默认的
phoneServerList.add(Context.TELEPHONY_SERVICE);
return phoneServerList;
}
public boolean endCall() {
boolean callSuccess = false;
ITelephony telephonyService = getTelephonyService();
try {
if (telephonyService != null) {
callSuccess = telephonyService.endCall();
}
} catch (RemoteException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
if (callSuccess == false) {
Executor eS = Executors.newSingleThreadExecutor();
eS.execute(new Runnable() {
@Override
public void run() {
disconnectCall();
}
});
callSuccess = true;
}
return callSuccess;
}
private boolean disconnectCall() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("service call phone 5 \n");
} catch (Exception exc) {
exc.printStackTrace();
return false;
}
return true;
}
// 使用endCall挂断不了,再使用killCall反射调用再挂一次
public static boolean killCall(Context context) {
try {
// Get the boring old TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
return false;
}
return true;
}
<uses-permission android:name="android.permission.CALL_PHONE" />
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有