android程序接通和挂断电话

这篇博客主要介绍了在Android中如何使用PhoneUtils类来实现电话的接通和挂断功能,并提醒读者在应用中添加必要的权限声明。
摘要由CSDN通过智能技术生成

关键代码:【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.inte
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值