Android拨打、接听、挂断电话操作

news/2024/11/15 4:13:36/

Android2.3之前的系统可以通过反射机制调用ITelephone的方法来挂断电话,因为Android2.3以后增加了对permission  android.permission.MODIFY_PHONE_STATE 的限制,之前的反射的方法不能用了,我们可以通过发送广播的方式来接听电话。

示例代码


package com.example.android_3gtest;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.KeyEvent;public class PhoneUtil {public static String TAG = PhoneUtil.class.getSimpleName();  /*** 挂断电话* @param context*/public static void endCall(Context context) {  try {  Object telephonyObject = getTelephonyObject(context);  if (null != telephonyObject) {  Class telephonyClass = telephonyObject.getClass();  Method endCallMethod = telephonyClass.getMethod("endCall");  endCallMethod.setAccessible(true);  endCallMethod.invoke(telephonyObject);  }  } catch (SecurityException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  }  }  private static Object getTelephonyObject(Context context) {  Object telephonyObject = null;  try {  // 初始化iTelephony  TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);  // Will be used to invoke hidden methods with reflection  // Get the current object implementing ITelephony interface  Class telManager = telephonyManager.getClass();  Method getITelephony = telManager.getDeclaredMethod("getITelephony");  getITelephony.setAccessible(true);  telephonyObject = getITelephony.invoke(telephonyManager);  } catch (SecurityException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  }  return telephonyObject;  }  /*** 通过反射调用的方法,接听电话,该方法只在android 2.3之前的系统上有效。* @param context*/  private static void answerRingingCallWithReflect(Context context) {  try {  Object telephonyObject = getTelephonyObject(context);  if (null != telephonyObject) {  Class telephonyClass = telephonyObject.getClass();  Method endCallMethod = telephonyClass.getMethod("answerRingingCall");  endCallMethod.setAccessible(true);  endCallMethod.invoke(telephonyObject);  // ITelephony iTelephony = (ITelephony) telephonyObject;  // iTelephony.answerRingingCall();  }  } catch (SecurityException e) {  e.printStackTrace();  } catch (IllegalArgumentException e) {  e.printStackTrace();  } catch (IllegalAccessException e) {  e.printStackTrace();  } catch (InvocationTargetException e) {  e.printStackTrace();  } catch (NoSuchMethodException e) {  e.printStackTrace();  }  }  /*** 伪造一个有线耳机插入,并按接听键的广播,让系统开始接听电话。* @param context*/  private static void answerRingingCallWithBroadcast(Context context){  AudioManager localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);  //判断是否插上了耳机  boolean isWiredHeadsetOn = localAudioManager.isWiredHeadsetOn();  if (!isWiredHeadsetOn) {  Intent headsetPluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG);  headsetPluggedIntent.putExtra("state", 1);  headsetPluggedIntent.putExtra("microphone", 0);  headsetPluggedIntent.putExtra("name", "");  context.sendBroadcast(headsetPluggedIntent);  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);  context.sendOrderedBroadcast(meidaButtonIntent, null);  Intent headsetUnpluggedIntent = new Intent(Intent.ACTION_HEADSET_PLUG);  headsetUnpluggedIntent.putExtra("state", 0);  headsetUnpluggedIntent.putExtra("microphone", 0);  headsetUnpluggedIntent.putExtra("name", "");  context.sendBroadcast(headsetUnpluggedIntent);  } else {  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);  context.sendOrderedBroadcast(meidaButtonIntent, null);  }  }  /*** 接听电话* @param context*/  public static void answerRingingCall(Context context) {  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {	//2.3或2.3以上系统answerRingingCallWithBroadcast(context);  } else {  answerRingingCallWithReflect(context);  }  }  /*** 打电话* @param context* @param phoneNumber*/  public static void callPhone(Context context, String phoneNumber) {  if(!TextUtils.isEmpty(phoneNumber)){  try {  Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+ phoneNumber));  context.startActivity(callIntent);  } catch (Exception e) {  e.printStackTrace();  }  }  }  /*** 拨电话* @param context* @param phoneNumber*/  public static void dialPhone(Context context, String phoneNumber){  if(!TextUtils.isEmpty(phoneNumber)){  try {  Intent callIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ phoneNumber));  context.startActivity(callIntent);  } catch (Exception e) {  e.printStackTrace();  }  }  }  
}





http://www.ppmy.cn/news/271053.html

相关文章

python安装使用Flask框架(Vscode)

编译器:VsCode,python3.** 首先安装,在终端输入 pip install flask安装成功后新建文件,app.python,创建一个简单的Web应用。 from flask import Flaskapp Flask(__name__)app.route(/) def hello():return Hello, …

Android中自动接听电话的功能

最近,需要完成一个自动化测试工具,来测试一些模块的功能,其中有些功能需要接通电话后才可以使用,因为这个工具是完全自动化的,所以需要处理自动接听电话 在4.1以前Android是提供了接口可以直接调用接听电话的&#xff…

android 监听电话状态 来电 接听 挂断

如果想要监听手机的来电状态 需要接收手机的电话广播 首先是静态注册 <receiver android:name".PhoneReceiver"android:exported"true"android:enabled"true"><intent-filter><action android:name"android.intent.actio…

Android 来电自动接听

1、闲聊 今天闲来无事&#xff0c;刚好一个朋友需要做一个来电自动接听的功能&#xff0c;我一想&#xff0c;咦&#xff0c;这尼玛我还没做过&#xff0c;好吧&#xff0c;去看看&#xff01;好吧&#xff0c;看就看吧那么我提来了&#xff0c;我该从哪儿入手呢&#xff1f;算…

Android 电话的反射调用机制实现静默接听电话

首先 建一个工程 insertTel 建一个com.android.internal.telephony的包 把系统的源码里的ITelephony.aidl拷贝进来 如下 ITelephony.aidl /** Copyright (C) 2007 The Android Open Source Project** Licensed under the Apache License, Version 2.0 (the "License&…

无人机编程donekit及通讯(二)

后起飞无人机 1、Jetson连接飞控 vehicle connect(/dev/ttyACM0, wait_readyTrue, baud921600) 串口连接&#xff0c;波频是921600 2、socket网络通信 ssocket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(("192.168.1.102",8080)) #绑定服务器的ip和端口…

Android 自动接听电话

1. android 2.3以下版本&#xff08;不包括2.3&#xff09; http://bbs.51cto.com/viewthread.php?tid1078059&extra&page1 中的“二 android低版本自动接听/挂断实现” 核心代码&#xff1a; Class<TelephonyManager> c TelephonyManager.class; …

无人驾驶介绍

美国机动工程师协会&#xff08;SAE&#xff09;和美国国家公路交通安全管理局&#xff08;NHTSA&#xff09;分别对自动驾驶等级做出划分。市面上主要采用的是SAE的说法&#xff0c;在L3级中当车辆提出接管需求时&#xff0c;驾驶者必须立刻接管车辆。当驾驶者无法满足L3级别自…