android实现接通和挂断电话

 更新时间:2018年05月05日 13:47:56   作者:WillenWu  
这篇文章主要为大家详细介绍了android实现接通和挂断电话功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android实现接通和挂断电话的具体代码,供大家参考,具体内容如下

关键代码:【PhoneUtils类】

package com.ebupt.phonerecorddemo.server; 
 
import java.lang.reflect.Method; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.view.KeyEvent; 
 
import com.android.internal.telephony.ITelephony; 
 
public class PhoneUtils { 
 static String TAG = "PhoneUtils"; 
 /** 
  * 从TelephonyManager中实例化ITelephony,并返回 
  */ 
 static public ITelephony getITelephony(TelephonyManager telMgr) 
   throws Exception { 
  Method getITelephonyMethod = telMgr.getClass().getDeclaredMethod( 
    "getITelephony"); 
  getITelephonyMethod.setAccessible(true);// 私有化函数也能使用 
  return (ITelephony) getITelephonyMethod.invoke(telMgr); 
 } 
  
 //自动接听 
 public static void autoAnswerPhone(Context c,TelephonyManager tm) { 
  try { 
   Log.i(TAG, "autoAnswerPhone"); 
   ITelephony itelephony = getITelephony(tm); 
   // itelephony.silenceRinger(); 
   itelephony.answerRingingCall(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
   try { 
    Log.e(TAG, "用于Android2.3及2.3以上的版本上"); 
    Intent intent = new Intent("android.intent.action.MEDIA_BUTTON"); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent); 
    c.sendOrderedBroadcast(intent, 
      "android.permission.CALL_PRIVILEGED"); 
    intent = new Intent("android.intent.action.MEDIA_BUTTON"); 
    keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    intent.putExtra("android.intent.extra.KEY_EVENT", keyEvent); 
    c.sendOrderedBroadcast(intent, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent1 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    localIntent1.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    localIntent1.putExtra("state", 1); 
    localIntent1.putExtra("microphone", 1); 
    localIntent1.putExtra("name", "Headset"); 
    c.sendOrderedBroadcast(localIntent1, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent2 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent1 = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    localIntent2.putExtra("android.intent.extra.KEY_EVENT", 
      localKeyEvent1); 
    c.sendOrderedBroadcast(localIntent2, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent3 = new Intent(Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent localKeyEvent2 = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    localIntent3.putExtra("android.intent.extra.KEY_EVENT", 
      localKeyEvent2); 
    c.sendOrderedBroadcast(localIntent3, 
      "android.permission.CALL_PRIVILEGED"); 
    Intent localIntent4 = new Intent(Intent.ACTION_HEADSET_PLUG); 
    localIntent4.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
    localIntent4.putExtra("state", 0); 
    localIntent4.putExtra("microphone", 1); 
    localIntent4.putExtra("name", "Headset"); 
    c.sendOrderedBroadcast(localIntent4, 
      "android.permission.CALL_PRIVILEGED"); 
   } catch (Exception e2) { 
    e2.printStackTrace(); 
    Intent meidaButtonIntent = new Intent( 
      Intent.ACTION_MEDIA_BUTTON); 
    KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, 
      KeyEvent.KEYCODE_HEADSETHOOK); 
    meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent); 
    c.sendOrderedBroadcast(meidaButtonIntent, null); 
   } 
  } 
 } 
  
 //自动挂断 
 public static void endPhone(Context c,TelephonyManager tm) { 
  try { 
   Log.i(TAG, "endPhone"); 
   ITelephony iTelephony; 
   Method getITelephonyMethod = TelephonyManager.class 
     .getDeclaredMethod("getITelephony", (Class[]) null); 
   getITelephonyMethod.setAccessible(true); 
   iTelephony = (ITelephony) getITelephonyMethod.invoke(tm, 
     (Object[]) null); 
   // 挂断电话 
   iTelephony.endCall(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
 
} 

需要用到的ITelephony.aidl:

package com.android.internal.telephony; 
 /** 
 * Interface used to interact with the phone. Mostly this is used by the 
 * TelephonyManager class. A few places are still using this directly. 
 * Please clean them up if possible and use TelephonyManager instead. 
 * {@hide} 
 */ 
 
interface ITelephony {  
 /**  
 * End call or go to the Home screen 
 * @return whether it hung up  
 */  
 boolean endCall();  
  
 /**  
 * Answer the currently-ringing call.  
 *  
 * If there's already a current active call, that call will be  
 * automatically put on hold. If both lines are currently in use, the  
 * current active call will be ended.  
 *  
 * TODO: provide a flag to let the caller specify what policy to use  
 * if both lines are in use. (The current behavior is hardwired to  
 * "answer incoming, end ongoing", which is how the CALL button  
 * is specced to behave.)  
 *  
 * TODO: this should be a oneway call (especially since it's called  
 * directly from the key queue thread).  
 */  
 void answerRingingCall(); 
  
 /** 
  * Silence the ringer if an incoming call is currently ringing. 
  * (If vibrating, stop the vibrator also.) 
  * 
  * It's safe to call this if the ringer has already been silenced, or 
  * even if there's no incoming call. (If so, this method will do nothing.) 
  * 
  * TODO: this should be a oneway call too (see above). 
  *  (Actually *all* the methods here that return void can 
  *  probably be oneway.) 
  */ 
 void silenceRinger(); 
  
 /** 
  * Allow mobile data connections. 
  */ 
 boolean enableDataConnectivity(); 
 
 /** 
  * Disallow mobile data connections. 
  */ 
 boolean disableDataConnectivity(); 
 
 /** 
  * Report whether data connectivity is possible. 
  */ 
 boolean isDataConnectivityPossible(); 
} 

监听通话广播【PhoneReceiver.java】:

package com.ebupt.phonerecorddemo.server; 
 
import android.app.Service; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
 
public class PhoneReceiver extends BroadcastReceiver { 
 String TAG = "PhoneReceiver"; 
 @Override 
 public void onReceive(Context context, Intent intent) { 
  TelephonyManager tm = (TelephonyManager) context 
    .getSystemService(Service.TELEPHONY_SERVICE); 
  switch (tm.getCallState()) { 
  case TelephonyManager.CALL_STATE_OFFHOOK:// 电话打进来接通状态;电话打出时首先监听到的状态。 
   Log.i("onCallStateChanged", "CALL_STATE_OFFHOOK"); 
   break; 
  case TelephonyManager.CALL_STATE_RINGING:// 电话打进来状态 
   Log.i("onCallStateChanged", "CALL_STATE_RINGING"); 
   PhoneUtils.autoAnswerPhone(context,tm); 
   break; 
  case TelephonyManager.CALL_STATE_IDLE:// 不管是电话打出去还是电话打进来都会监听到的状态。 
   Log.i("onCallStateChanged", "CALL_STATE_IDLE"); 
   break; 
  } 
 } 
 
  
} 

在上面类适当的地方加上挂断或接通电话的代码即可。

最后别忘记在AndroidManifest.xml里声明和注册权限。

<uses-permission android:name="android.permission.READ_PHONE_STATE" > 
</uses-permission> 
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" > 
</uses-permission> 
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<receiver android:name="com.ebupt.phonerecorddemo.server.PhoneReceiver" > 
 <intent-filter android:priority="2147483647" > 
 <action android:name="android.intent.action.PHONE_STATE" > 
 </action> 
 </intent-filter> 
</receiver> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android dialog 去除虚拟按键的解决方法

    Android dialog 去除虚拟按键的解决方法

    今天小编就为大家分享一篇Android dialog 去除虚拟按键的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Android选择图片或视频进行循环播放

    Android选择图片或视频进行循环播放

    这篇文章主要为大家详细介绍了Android选择图片或视频进行循环播放,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android实现简易版打地鼠

    Android实现简易版打地鼠

    这篇文章主要为大家详细介绍了Android实现简易版打地鼠,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • Android自定义简单的顶部标题栏

    Android自定义简单的顶部标题栏

    这篇文章主要为大家详细介绍了Android自定义简单的顶部标题栏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-11-11
  • android实现短按电源键关机的实现代码

    android实现短按电源键关机的实现代码

    这篇文章主要介绍了android实现短按电源键关机的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Kotlin学习教程之函数的默认参数

    Kotlin学习教程之函数的默认参数

    这篇文章主要给大家介绍了关于Kotlin学习教程之函数的默认参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Android TextView的TextWatcher使用案例详解

    Android TextView的TextWatcher使用案例详解

    这篇文章主要介绍了Android TextView的TextWatcher使用案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Android布局自定义Shap圆形ImageView可以单独设置背景与图片

    Android布局自定义Shap圆形ImageView可以单独设置背景与图片

    这篇文章主要介绍了Android布局自定义Shap圆形ImageView可以单独设置背景与图片 的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android实现简单画中画功能

    Android实现简单画中画功能

    这篇文章主要为大家详细介绍了Android实现简单画中画功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • 手把手教你用Android自定义饼状图

    手把手教你用Android自定义饼状图

    最近在项目中需要用到简单的饼形图统计,我们就没必要去找什么开源的,可以尝试自己画一下。现在将实现的过程分享给大家,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-10-10

最新评论