`

android 来电自动接听和自动挂断

 
阅读更多

android 来电自动接听和自动挂断

 

 

注意:android2.3版本不支持下面的自动接听方法。(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current process has android.permission.MODIFY_PHONE_STATE.)

 

 

第一步:准备应用环境需要的系统包和aidl文件。

 

(1)在应用中创建包:android.telephony

将android系统框架下的\framework\telephony\java\android\telephony目录中的NeighboringCellInfo.aidl文件复制到上面创建的包(android.telephony )中;

(2)在应用中创建包:com.android.internal.telephony

将android系统框架下的\framework\telephony\java\com\android\internal\telephony目录中的ITelephony.aidl文件复制到上面创建的包(com.android.internal.telephony )中;

 

 

第二步:创建一个获取ITelephony的方法

PhoneUtils.java

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import java.lang.reflect.Method;  
  4. import com.android.internal.telephony.ITelephony;  
  5. import android.telephony.TelephonyManager;  
  6.   
  7. public class PhoneUtils {  
  8.     /** 
  9.      * 根据传入的TelephonyManager来取得系统的ITelephony实例. 
  10.      * @param telephony 
  11.      * @return 系统的ITelephony实例 
  12.      * @throws Exception 
  13.      */  
  14.     public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {   
  15.         Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");   
  16.         getITelephonyMethod.setAccessible(true);//私有化函数也能使用   
  17.         return (ITelephony)getITelephonyMethod.invoke(telephony);   
  18.     }  
  19. }  
 

第三步:创建电话广播拦截器

MyPhoneBroadcastReceiver.java

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import com.android.internal.telephony.ITelephony;  
  4. import android.content.BroadcastReceiver;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.telephony.TelephonyManager;  
  8. import android.util.Log;  
  9.   
  10. public class MyPhoneBroadcastReceiver extends BroadcastReceiver {  
  11.   
  12.     private final static String TAG = MyPhone.TAG;  
  13.       
  14.     @Override  
  15.     public void onReceive(Context context, Intent intent) {  
  16.         String action = intent.getAction();  
  17.         Log.i(TAG, "[Broadcast]"+action);  
  18.           
  19.         //呼入电话  
  20.         if(action.equals(MyPhone.B_PHONE_STATE)){  
  21.             Log.i(TAG, "[Broadcast]PHONE_STATE");  
  22.             doReceivePhone(context,intent);  
  23.         }  
  24.     }  
  25.       
  26.     /** 
  27.      * 处理电话广播. 
  28.      * @param context 
  29.      * @param intent 
  30.      */  
  31.     public void doReceivePhone(Context context, Intent intent) {  
  32.         String phoneNumber = intent.getStringExtra(  
  33. TelephonyManager.EXTRA_INCOMING_NUMBER);  
  34.         TelephonyManager telephony = (TelephonyManager)context.getSystemService(  
  35. Context.TELEPHONY_SERVICE);  
  36.         int state = telephony.getCallState();  
  37.           
  38.         switch(state){  
  39.         case TelephonyManager.CALL_STATE_RINGING:  
  40.             Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);  
  41.             try {  
  42.                 ITelephony iTelephony = PhoneUtils.getITelephony(telephony);  
  43.                 iTelephony.answerRingingCall();//自动接通电话  
  44.                 //iTelephony.endCall();//自动挂断电话  
  45.             } catch (Exception e) {  
  46.                 Log.e(TAG, "[Broadcast]Exception="+e.getMessage(), e);  
  47.             }  
  48.             break;  
  49.         case TelephonyManager.CALL_STATE_IDLE:  
  50.             Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);  
  51.             break;  
  52.         case TelephonyManager.CALL_STATE_OFFHOOK:  
  53.             Log.i(TAG, "[Broadcast]通话中="+phoneNumber);  
  54.             break;  
  55.         }  
  56.     }  
  57.   
  58. }  
 

 

第四部:注册电话广播拦截器

MyPhone.java

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.IntentFilter;  
  5. import android.os.Bundle;  
  6. import android.telephony.TelephonyManager;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9.   
  10. public class MyPhone extends Activity {  
  11.     public final static String TAG = "MyPhone";  
  12.       
  13.     public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;  
  14.       
  15.     private MyPhoneBroadcastReceiver mBroadcastReceiver;  
  16.       
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.my_phone);  
  21.     }  
  22.       
  23.     //按钮1-注册广播  
  24.     public void registerThis(View v) {  
  25.         Log.i(TAG, "registerThis");  
  26.         mBroadcastReceiver = new MyPhoneBroadcastReceiver();  
  27.         IntentFilter intentFilter = new IntentFilter();  
  28.         intentFilter.addAction(B_PHONE_STATE);  
  29.         intentFilter.setPriority(Integer.MAX_VALUE);  
  30.         registerReceiver(mBroadcastReceiver, intentFilter);  
  31.     }  
  32.       
  33.     //按钮2-撤销广播  
  34.     public void unregisterThis(View v) {  
  35.         Log.i(TAG, "unregisterThis");  
  36.         unregisterReceiver(mBroadcastReceiver);  
  37.     }  
  38.       
  39. }  
 

第5步:在AndroidManifest.xml配置权限

Xml代码   收藏代码
  1. <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  2. <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />  
  3. <uses-permission android:name="android.permission.CALL_PHONE"/>  
 

其中:

Java代码   收藏代码
  1. iTelephony.answerRingingCall();//自动接通电话  

 必须有权限 android.permission.MODIFY_PHONE_STATE

 

Java代码   收藏代码
  1. iTelephony.endCall();//自动挂断电话  

 必须有权限 android.permission.CALL_PHONE

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics