• 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代码 

    package com.zhouzijing.android.demo;
    import java.lang.reflect.Method;
    import com.android.internal.telephony.ITelephony;
    import android.telephony.TelephonyManager;

    public class PhoneUtils {
      /**
      * 根据传入的TelephonyManager来取得系统的ITelephony实例.
      * @param telephony
      * @return 系统的ITelephony实例
      * @throws Exception
      */
      public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
        Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
        getITelephonyMethod.setAccessible(true);//私有化函数也能使用
        return (ITelephony)getITelephonyMethod.invoke(telephony);
      }
    }

     

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

    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代码 
      <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
        <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
      <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

  • 相关阅读:
    转】主流PHP框架间的比较(Zend Framework,CakePHP,CodeIgniter,Symfony,ThinkPHP,FleaPHP)
    总结 沉寂了大半年后的又一次
    xampp phpmyadmin产生403错误的解决办法
    Codeigniter 去除URL中的index.php
    【分享】C#中abstract与virtual的区别
    SQL Server 中使用Convert来取得datetime数据类型样式(全)
    Visual Studio 2012 RTM全系列简体中文正式版(附注册码)
    VS2008的Web.config、XML文件无法高亮显示的问题解决方案
    循环类里面的每一个属性
    Eclipse 打开已存在的Android项目的问题
  • 原文地址:https://www.cnblogs.com/DswCnblog/p/2681748.html
Copyright © 2020-2023  润新知