1、首先在manifest中加入如下的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" >
    </uses-permission>
    <uses-permission android:name="android.permission.CALL_PHONE" >
    </uses-permission>

红色的部分如果报错,则clean一下就好了。

<activity
            android:name="com.example.tel.phonecall.PhoneCall"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.tel.phonecall.ShowAct"
            android:launchMode="singleInstance" >
        </activity>

<receiver
            android:name="com.example.tel.phonecall.PhoneListener"
            android:permission="android.permission.PROCESS_OUTGOING_CALLS" >
            <intent-filter android:priority="-1000" >
                <action android:name="android.intent.action.PHONE_STATE" >
                </action>
                <category android:name="android.intent.category.DEFAULT" >
                </category>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" >
                </action>
                <category android:name="android.intent.category.DEFAULT" >
                </category>
            </intent-filter>
        </receiver>

2、代码如下:

package com.example.tel.phonecall;
import com.example.tel.R;
import android.app.Activity;
import android.os.Bundle;

public class PhoneCall extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

}

package com.example.tel.phonecall;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class PhoneListener extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// LogOut.out(this, "ord:" + isOrderedBroadcast() + " act:" + action);
Log.i(this.getClass().getName(), "ord:" + isOrderedBroadcast()
+ " act:" + action);
Toast toast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
if (action.equals("android.intent.action.NEW_OUTGOING_CALL")) {
toast.setText("Outgoing");
} else {
toast.setText("InComing");
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Service.TELEPHONY_SERVICE);
boolean incomingFlag = false;
String incoming_number = "";
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;// 标识当前是来电
incoming_number = intent.getStringExtra("incoming_number");
Log.i(this.getClass().getName(), "RINGING :" + incoming_number);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent tmpI = new Intent(context, ShowAct.class);
tmpI.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(tmpI);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (incomingFlag) {
Log.i(this.getClass().getName(), "incoming ACCEPT :"
+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (incomingFlag) {
Log.i(this.getClass().getName(), "incoming IDLE");
}
break;
}
}
toast.show();
}
}

package com.example.tel.phonecall;

import java.lang.reflect.Method;
import com.example.tel.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.android.internal.telephony.ITelephony;

public class ShowAct extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setWindowAnimations(0);
setContentView(R.layout.show);
Button btnRefuse = (Button) findViewById(R.id.btnEndcall);
btnRefuse.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
endCall();
}
});
Button btnReceiver = (Button) findViewById(R.id.btnAnswer);
btnReceiver.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
// answerCall();
// answerRingingCall(getApplication());
answerRingingCallWithBroadcast(getApplication());
}
});
}

private void endCall() {
TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Class<TelephonyManager> c = TelephonyManager.class;
Method mthEndCall = null;
try {
mthEndCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
mthEndCall.setAccessible(true);
ITelephony iTel = (ITelephony) mthEndCall.invoke(telMag,
(Object[]) null);
iTel.endCall();
Log.i(this.getClass().getName(), iTel.toString());
} catch (Exception e) {
e.printStackTrace();
}
Log.i(this.getClass().getName(), "endCall test");
}

private void answerRingingCallWithBroadcast(Context context) {
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
// 判断是否插上了耳机
if (!audioManager.isWiredHeadsetOn()) {
// 4.1以上系统限制了部分权限, 使用三星4.1版本测试提示警告:Permission Denial: not allowed to
// send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
// uid=10017
// 这里需要注意一点,发送广播时加了权限“android.permission.CALL_PRIVLEGED”,
//则接受该广播时也需要增加该权限。但是4.1以上版本貌似这个权限只能系统应用才可以得到。测试的时候,
//自定义的接收器无法接受到此广播,后来去掉了这个权限,设为NULL便可以监听到了。
if (android.os.Build.VERSION.SDK_INT >= 15) {
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);
} else {
// 以下适用于Android2.3及2.3以上的版本上 ,但测试发现4.1系统上不管用。
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");
context.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(Intent.EXTRA_KEY_EVENT, localKeyEvent1);
context.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(Intent.EXTRA_KEY_EVENT, localKeyEvent2);
context.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");
context.sendOrderedBroadcast(localIntent4,
"android.permission.CALL_PRIVILEGED");
}
} 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);
}
}

public void onStart() {
super.onStart();
}

public void onPause() {
super.onPause();
finish();
}
}

3、ITelephony.aidl

package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
}

Android开发教程--自定义接听/挂断电话功能相关推荐

  1. android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例

    本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...

  2. android 自动拨打电话和挂断电话(反射和intent方式)

    欢迎来到博主的技术分享博客 今天分享的技术主要内容是自动拨打电话和挂断电话.自动拨打电话分为两种,一种是通过反射ITelephony这个类直接调用call方法,一种是通过intent方法.而挂断电话则 ...

  3. android蓝牙耳机来电铃声,Android蓝牙耳机接听挂断电话流程

    一.alps/packages/apps/Bluetooth/src/com/android/bluetooth/hfp/HeadsetStateMachine.java image.png proc ...

  4. Xamarin.Android实现自动拨打及挂断电话的功能

    目录 1.说明 2.具体实现 2.1 效果 2.2 原理说明 2.3 具体代码实现 2.3.1 layout 2.3.3 广播接收器 2.3.3 拨打电话的Activity 2.3.4 主界面Acti ...

  5. Android自动接听和挂断电话实现原理

    转自:http://bbs.51cto.com/thread-1078059-1.html 一  前言 这两天要研究类似白名单黑名单以及手势自动接听的一些功能,所以呢,自然而然的涉及到怎么自动接听/挂 ...

  6. android 通话状态监听(自定义接听挂断按钮与通话界面,根据公司的业务逻辑可以实现自己的来电秀功能)...

    前言: 因为公司需求,要自定义一款来电秀的app当做周边产品来配合主营的app业务. 之前因为赶项目,没时间整理这块,现在项目告一段落了,现在回头看看感觉这个功能还是挺有意思的,比较有针对性.电话呼入 ...

  7. Android挂断电话流程

    近期在友盟上看到许多关于挂断电话导致崩溃的问题,如下异常 java.lang.NoSuchMethodError: No interface method endCall()Z in class Lc ...

  8. Android之——自动挂断电话的实现

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47072451 通过<Android之--AIDL小结>与<And ...

  9. Android通过AIDL实现接听电话、挂断电话操作 | 拨打电话

    Android中拨打电话功能是很轻松的只需要几行代码即可,如下: 首先还是得加上权限这个东西(或者动态申请) <!--拨打电话--> <uses-permission android ...

最新文章

  1. Java中使用递归算法实现子级架构的查询
  2. 几种常见的电话光端机连接图以及电话光端机的连接方式
  3. 计算机丢失shlwapi,请教高手提示shlwapi.dll错误应该怎么处理?
  4. 【图像风格转换】项目参考资料总结
  5. 用excel 2016连接mysql_excel导入mysql数据库方法(最新,2016年2月19日)
  6. linux0.11内核视频讲解,linux0.11内核分析-setup
  7. c语言string最大长度,求3个字符串中最长单词的长度 求救 会一个的
  8. Mybatis Generator 自动生成数据库XML(Mybatis Generator 逆向工程)
  9. linux自动更新漏洞,Linux爆本地提权漏洞 请立即更新udev程序
  10. 谷歌分析中的营销归因分析功能
  11. java 设置 cors,Spring MVC配置CORS
  12. python数据分析
  13. 离散数学班委竞选问题用python程序实现(课本p45.30)
  14. 对一支圆珠笔进行测试,要从哪些方面进行测试?
  15. 常见算法:C语言求素数的问题
  16. 东邪西毒经典对白,程序员版
  17. 【无标题】不出门怎么连接公司电脑,这6个工具让你不用来回跑。
  18. VR制作中必须踩的坑365之037(oculus2、UE4、UE5、VR记录一年的踩坑之旅)Maya / ZBrush / Substance Painter倒来倒去
  19. MySQL三表查询(学生表、课程表、成绩表)查询出语文成绩比数学成绩高的学生信息
  20. jQuery树形控件zTree使用小结

热门文章

  1. 好程序员云计算教程分享入门云计算要精通学习什么?
  2. python编程设计大学ppt_上海交通大学-python程序设计课程PPT-Ch7-(1)...ppt
  3. 基于MATLAB的中继系统在瑞利信道下的误码率仿真分析
  4. 轴向柱塞泵体加工生产线专机及主要设备多头钻床及攻丝机床液压系统设计(论文 CAD图纸 液压系统图 工序卡)
  5. 西子奥的斯服务器光电信号,西子奥的斯怎么看故障
  6. linux如deepin manjaro对笔记本电脑电池的伤害解决方案:TLP:一个可以延长 Linux 笔记本电池寿命的高级电源管理工具
  7. 前端单元测试怎么写(以Vue为例)
  8. 拒绝攀比 理性分期消费
  9. js导出excel数据,图片,身份证号码
  10. java 对应sql驱动版本_有关sqlserver的 jdbc驱动版本整理