android 自动挂断,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里声明和注册权限。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
android 自动挂断,android实现接通和挂断电话相关推荐
- android 自动 键盘,关于Android中的软键盘
InputMethodService为我们的输入法创建了一个Dialog,并且将该Dialog的Window的某些参数(如Gravity)进行了设置,使之能够在底部或者全屏显示.当我们点击输入框时,系 ...
- android自动更新demo,Android程序自动更新功能模块的实现方法【附完整demo源码下载】...
本文实例讲述了Android程序自动更新功能模块的实现方法.分享给大家供大家参考,具体如下: 在程序启动的时候检测服务器上有没有对应版本更新,如果有更新,提示用户是否更新. 在程序启动的时候首先调用更 ...
- android自动创建快捷方式,Android开发之生成桌面快捷方式细则(原创)
本文已独家授权 郭霖 ( guolin_blog) 公众号发布! 申明,标题里的快捷方式不是指开发人员使用频率极高的Ctrl+C和Ctrl+V:也不是IDE里Ctrl+D.Ctrl+F等常用快捷键.这 ...
- android自动夜间模式,Android实现日夜间模式的深入理解
在本篇文章中给出了三种实现日间/夜间模式切换的方案,三种方案综合起来可能导致文章的篇幅过长,请耐心阅读. 1.使用 setTheme的方法让 Activity重新设置主题: 2.设置 Android ...
- android自动切换图片,Android应用中图片浏览时实现自动切换功能的方法详解
先给最终效果图: 当我们在最下边的gallery中切换图片时,上面的大图片会自动切换,切换时有动画效果哦,很简单的一个程序,有待完善更多的功能! activity代码: package cn.com. ...
- Android 自动搜索频道,Android自定义收音机搜台控件RadioRulerView
前言:像这类的自定义控件有非常多的开源项目,但还是没有找到我项目想要的,所以简单实现了一个,下面简单讲讲实现原理. 效果图: 实现思路: 首先画固定背景尺子,而实现这个则要计算刻度线的宽度.刻度线间的 ...
- android自动添加包,android nfc写入应用程序包名与网址 自动打开应用程序与网址...
安卓手机大部分手机提供了NFC芯片 可以根据NFC传递数据 NFC卡与手机 手机与手机数据等交互 非常好的功能,这篇文章教大家如何向NFC卡或标签写入功能. 用法 : 点击包名 将N ...
- android 自动播放 幻灯片,Android自动播放Banner图片轮播效果
本文实例为大家分享了Android自动播放Banner图片轮播的具体代码,供大家参考,具体内容如下 先看一下效果图 支持本地图片以及网络图片or本地网络混合. 使用方式: android:id=&qu ...
- android 自动静音模式,Android实现定时自动静音小助手
定时静音助手的实现方法,供大家参考,具体内容如下 背景 突发奇想,刚好这学期刚上安卓课程,想设计一个时间助手.工作.学习中经常会被突如其来的电话所打扰,在上班,上课时这突如其来的铃声会惹来别人的反感, ...
- Android自动伸展动画,android – 如何实现平滑的展开/折叠动画
我指的是在这里找到的扩展/折叠动画代码. 虽然这样做不好,动画不顺利. 我做一些登录代码. public static void expand(final View v) { v.measure(Me ...
最新文章
- 7款外观迷人的HTML5/CSS3 3D特效按钮特效
- 定位相关论文-A Novel Pedestrian Dead Reckoning Algorithm for Multi-Mode Recognition Based on Smartphones
- python有哪些常用的库
- 数据对象、属性和相似性
- 【乐理知识】第二章 音符 休止符
- 31. HTTP 与 HTTPS 区别
- [MATLAB]数值计算
- 【分析】云硬盘的Burst功能
- [翻译] Oracle Database 12c 新特性Multitenant - Cheney Shue
- Vue 钩子函数activated未触发
- 操作系统--多线程进阶(上)
- Ubuntu1804 使用mondorescue 进行系统备份iso制作
- vscode 设置setting文件
- 中国石油大学(北京)-《外国文学作品选读》在线考试
- 大数据系列 之 学习准备
- 信息安全主题的高清PPT背景图110张,打包下载
- 全国二级c语言上机题库答案,二级C语言上机题库100题加答案
- SpringBoot如何配置log4j输出日志呢?
- 魔方CFOP公式匹配算法
- 个人项目-地铁出行路线
热门文章
- 关于 .NET Core(.NET Core 指南)
- Elixir: 魔术符号
- 电大计算机与英语模拟测试题,2014年9月大学英语B统考 电大 网院网考模拟试题4...
- iOS —— 触摸事件传递及响应与手势
- 深度式睡眠潜入虚拟世界_潜入swiftui的惊人世界
- 绿色城市智慧运营:Web 3D 垃圾分类 GIS 系统
- 墨子号量子计算机原型时间,科学网—“墨子号”量子卫星实现安全时间传递
- mysql字符集以及字符集错误
- 魔兽世界最新服务器推荐,魔兽世界服务器人数最新数据!萌新入坑,老玩家转服慎重选择!...
- Unity ECS实现RTS游戏中的游戏单位框选、集结和移动控制