这个需要自己写个监听服务,在接收到来电时进行处理,监听服务和播放录音方面的代码比较简单自己可以摸索一下
下面附上用到的接电话和挂电话的代码:
[代码]java代码:
01 | /** |
02 | * 接听电话功能类 |
03 | * |
04 | * 权限: |
05 | * <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/> |
06 | */ |
07 | public class PhoneUtils { |
08 | private static final int ANSWER_RINGING_CALL = 0x01 ; |
09 | private static final int SILENCE_RINGER = 0x02 ; |
10 | private static final int HANG_UP = 0x03 ; |
11 | /** |
12 | * 接电话 |
13 | * @param currentActivity 当前的Activity.this |
14 | */ |
15 | public static void answerCall(Activity currentActivity) { |
16 | doAction(currentActivity, ANSWER_RINGING_CALL); |
17 | } |
18 | /** |
19 | * 静音 |
20 | * @param currentActivity 当前的Activity.this |
21 | */ |
22 | public static void silenceRinger(Activity currentActivity) { |
23 | doAction(currentActivity, SILENCE_RINGER); |
24 | } |
25 | /** |
26 | * 挂电话 |
27 | * @param currentActivity 当前的Activity.this |
28 | */ |
29 | public static void hangUp(Activity currentActivity) { |
30 | doAction(currentActivity, HANG_UP); |
31 | } |
32 | private static void doAction(Activity current, int what) { |
33 | TelephonyManager phoneManager = (TelephonyManager)current.getSystemService( "phone" ); |
34 | Class phoneManagerClass = phoneManager.getClass(); |
35 | try { |
36 | Method getITelephony = phoneManagerClass.getDeclaredMethod( "getITelephony" , new Class[]{}); |
37 | getITelephony.setAccessible( true ); |
38 | //ITelephony |
39 | Object insITelephony = getITelephony.invoke(phoneManager, new Object[]{}); |
40 | Class ITelephony = insITelephony.getClass(); |
41 | switch (what) { |
42 | case ANSWER_RINGING_CALL: |
43 | Method answerRingingCall = ITelephony.getMethod( "answerRingingCall" , new Class[]{}); |
44 | answerRingingCall.invoke(insITelephony, new Object[]{}); |
45 | break ; |
46 | case SILENCE_RINGER: |
47 | Method silenceRinger = ITelephony.getMethod( "silenceRinger" , new Class[]{}); |
48 | silenceRinger.invoke(insITelephony, new Object[]{}); |
49 | break ; |
50 | case HANG_UP: |
51 | Method endCall = ITelephony.getMethod( "endCall" , new Class[]{}); |
52 | endCall.invoke(insITelephony, new Object[]{}); |
53 | break ; |
54 | } |
55 | } catch (Exception e) { |
56 | e.printStackTrace(); |
57 | } |
58 | } |
59 | private PhoneUtils(){} |
60 | } |