扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
package com.android.internal.telephony;
interface ITelephony {
void answerRingCall();
boolean endCall();
}
MyPhoneService
创新互联于2013年开始,是专业互联网技术服务公司,拥有项目网站设计、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元湛江做网站,已为上家服务,为湛江各地企业和个人服务,联系电话:18982081108public class MyPhoneService extends Service {
TelephonyManager manager;
ITelephony telephony;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
run();
return super.onStartCommand(intent, flags, startId);
}
private void run() {
manager= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// 获取类类型 Class clazz = TelephonyManager.class;
try {
// 通过方法名和参数找到方法 Method method = clazz.getDeclaredMethod("getITelephony", null);
// 将此方法设为可用 method.setAccessible(true);
// 执行该方法 telephony = (ITelephony) method.invoke(manager, null);
}catch (NoSuchMethodException e) {
e.printStackTrace();
}catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (IllegalAccessException e) {
e.printStackTrace();
}catch (InvocationTargetException e) {
e.printStackTrace();
}
// 设置电话状态监听 PhoneStateListener listener = new MyPhoneStateListener();
manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:// 当摘机时 Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK"
+ incomingNumber);
try {
telephony.endCall();
}catch (RemoteException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_RINGING:// 当响铃时 Log.i("onCallStateChanged", "CALL_STATE_RINGING"
+ incomingNumber);
try {
// 接电话 telephony.answerRingCall();
}catch (RemoteException e) {
e.printStackTrace();
}
break;
case TelephonyManager.CALL_STATE_IDLE: // 当空闲时 Log.i("onCallStateChanged", "CALL_STATE_IDLE" + incomingNumber);
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流