码迷,mamicode.com
首页 > 移动开发 > 详细

android怎么实现自动接听和挂断电话功能

时间:2015-10-30 12:29:46      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:

原文地址:http://zhidao.baidu.com/link?url=LVQ9uI0sHUWtKOroOLc21aaAWOuJDZnqL4TXfwF5bQOUqCULMl3Ayzg5BBY29CuqQVcyvAY6DlrYpmUhyoiNGkOJhn1t37SZCmdrSNyIyby

 

Android自动接听和挂断电话实现原理http://blog.csdn.net/chenda_lin/article/details/41346493

Android提供的系统服务之--TelephonyManager(电话管理器)http://www.2cto.com/kf/201410/347844.html

android 实现来电自动接听和自动挂断的方法:
第一步:准备应用环境需要的系统包和aidl文件。
(1)在应用中创建包:android.telephony
将android系统框架下的\framework\telephony\java\android\telephony目录中的NeighboringCellInfo.aidl文件复制到上面创建的包(android.telephony )中;
(2)在应用中创建包:com.android.internal.telephony
将 android系统框架下的\framework\telephony\java\com\android\internal\telephony目录中 的ITelephony.aidl文件复制到上面创建的包(com.android.internal.telephony )中。
第二步:创建一个获取ITelephony的方法
PhoneUtils.java
package com.zhouzijing.android.demo;

import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;

public class PhoneUtils {
/**
* 根据传入的TelephonyManager来取得系统的ITelephony实例.
* @param telephony
* @return 系统的ITelephony实例
* @throws Exception
*/
public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {
Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);//私有化函数也能使用
return (ITelephony)getITelephonyMethod.invoke(telephony);
}
}

第三步:创建电话广播拦截器
MyPhoneBroadcastReceiver.java
package com.zhouzijing.android.demo;

import com.android.internal.telephony.ITelephony;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneBroadcastReceiver extends BroadcastReceiver {

private final static String TAG = MyPhone.TAG;

@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(TAG, "[Broadcast]"+action);

//呼入电话
if(action.equals(MyPhone.B_PHONE_STATE)){
Log.i(TAG, "[Broadcast]PHONE_STATE");
doReceivePhone(context,intent);
}
}

/**
* 处理电话广播.
* @param context
* @param intent
*/
public void doReceivePhone(Context context, Intent intent) {
String phoneNumber = intent.getStringExtra(
TelephonyManager.EXTRA_INCOMING_NUMBER);
TelephonyManager telephony = (TelephonyManager)context.getSystemService(
Context.TELEPHONY_SERVICE);
int state = telephony.getCallState();

switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);
try {
ITelephony iTelephony = PhoneUtils.getITelephony(telephony);
iTelephony.answerRingingCall();//自动接通电话
//iTelephony.endCall();//自动挂断电话
} catch (Exception e) {
Log.e(TAG, "[Broadcast]Exception="+e.getMessage(), e);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(TAG, "[Broadcast]通话中="+phoneNumber);
break;
}
}

}

第四部:注册电话广播拦截器
MyPhone.java
package com.zhouzijing.android.demo;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;

public class MyPhone extends Activity {
public final static String TAG = "MyPhone";

public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;

private MyPhoneBroadcastReceiver mBroadcastReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_phone);
}

//按钮1-注册广播
public void registerThis(View v) {
Log.i(TAG, "registerThis");
mBroadcastReceiver = new MyPhoneBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(B_PHONE_STATE);
intentFilter.setPriority(Integer.MAX_VALUE);
registerReceiver(mBroadcastReceiver, intentFilter);
}

//按钮2-撤销广播
public void unregisterThis(View v) {
Log.i(TAG, "unregisterThis");
unregisterReceiver(mBroadcastReceiver);
}

}

第5步:在AndroidManifest.xml配置权限
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>

其中:
iTelephony.answerRingingCall();//自动接通电话
必须有权限 android.permission.MODIFY_PHONE_STATE
iTelephony.endCall();//自动挂断电话
必须有权限 android.permission.CALL_PHONE。

 

添加权限
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas。android。com/apk/res/android"
androidrientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RadioGroup android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/rGrpSelect">
<RadioButton android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/rbtnAutoAccept"
android:text="所有来电自动接听"></RadioButton>
<RadioButton android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/rbtnAutoReject"
android:text="所有来电自动挂断"></RadioButton>
</RadioGroup>
<ToggleButton android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/tbtnRadioSwitch"
android:textOn="Radio已经启动" android:textOff="Radio已经关闭"
android:textSize="24dip" android:textStyle="normal"></ToggleButton>
<ToggleButton android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/tbtnDataConn"
android:textSize="24dip" android:textStyle="normal" android:textOn="允许数据连接"
android:textOff="禁止数据连接"></ToggleButton>
</LinearLayout>

PhoneUtils.java是手机功能类,从TelephonyManager中实例化ITelephony并返回,源码如下:
package com.testTelephony;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import com.android.internal.telephony.ITelephony;
import android.telephony.TelephonyManager;
import android.util.Log;

public class 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);
}

static public void printAllInform(Class clsShow) {
try {
// 取得所有方法
Method[] hideMethod = clsShow.getDeclaredMethods();
int i = 0;
for (; i < hideMethod.length; i++) {
Log.e("method name", hideMethod.getName());
}
// 取得所有常量
Field[] allFields = clsShow.getFields();
for (i = 0; i < allFields.length; i++) {
Log.e("Field name", allFields.getName());
}
} catch (SecurityException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (IllegalArgumentException e) {
// throw new RuntimeException(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
testTelephony.java是主类,使用PhoneStateListener监听通话状态,以及实现上述4种电话控制功能,源码如下:
package com.testTelephony;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.ToggleButton;

public class testTelephony extends Activity {
/** Called when the activity is first created. */
RadioGroup rg;//来电操作单选框
ToggleButton tbtnRadioSwitch;//Radio开关
ToggleButton tbtnDataConn;//数据连接的开关
TelephonyManager telMgr;
CallStateListener stateListner;
int checkedId=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

telMgr= (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
telMgr.listen(new CallStateListener(), CallStateListener.LISTEN_CALL_STATE);

PhoneUtils.printAllInform(TelephonyManager.class);

rg = (RadioGroup)findViewById(R.id.rGrpSelect);
rg.setOnCheckedChangeListener(new CheckEvent());
tbtnRadioSwitch=(ToggleButton)this.findViewById(R.id.tbtnRadioSwitch);
tbtnRadioSwitch.setOnClickListener(new ClickEvent());
try {
tbtnRadioSwitch.setChecked(PhoneUtils.getITelephony(telMgr).isRadioOn());
} catch (Exception e) {
Log.e("error",e.getMessage());
}
tbtnDataConn=(ToggleButton)this.findViewById(R.id.tbtnDataConn);
tbtnDataConn.setOnClickListener(new ClickEvent());
try {
tbtnDataConn.setChecked(PhoneUtils.getITelephony(telMgr).isDataConnectivityPossible());
} catch (Exception e) {
Log.e("error",e.getMessage());
}
}

/**
* 来电时的操作
* @author GV
*
*/
public class CheckEvent implements RadioGroup.OnCheckedChangeListener{

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
testTelephony.this.checkedId=checkedId;
}
}

/**
* Radio和数据连接的开关
* @author GV
*
*/
public class ClickEvent implements View.OnClickListener{

@Override
public void onClick(View v) {
if (v == tbtnRadioSwitch) {
try {
PhoneUtils.getITelephony(telMgr).setRadio(tbtnRadioSwitch.isChecked());
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
else if(v==tbtnDataConn){
try {
if(tbtnDataConn.isChecked())
PhoneUtils.getITelephony(telMgr).enableDataConnectivity();
else if(!tbtnDataConn.isChecked())
PhoneUtils.getITelephony(telMgr).disableDataConnectivity();
} catch (Exception e) {
Log.e("error", e.getMessage());
}
}
}
}

/**
* 监视电话状态
* @author GV
*
*/
public class CallStateListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(state==TelephonyManager.CALL_STATE_IDLE)//挂断
{
Log.e("IDLE",incomingNumber);
}
else if(state==TelephonyManager.CALL_STATE_OFFHOOK)//接听
{
Log.e("OFFHOOK",incomingNumber);
}
else if(state==TelephonyManager.CALL_STATE_RINGING)//来电
{
if(testTelephony.this.checkedId==R.id.rbtnAutoAccept)
{
try {
//需要<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
PhoneUtils.getITelephony(telMgr).silenceRinger();//静铃
PhoneUtils.getITelephony(telMgr).answerRingingCall();//自动接听

} catch (Exception e) {
Log.e("error",e.getMessage());
}
}
else if(testTelephony.this.checkedId==R.id.rbtnAutoReject)
{
try {
PhoneUtils.getITelephony(telMgr).endCall();//挂断
PhoneUtils.getITelephony(telMgr).cancelMissedCallsNotification();//取消未接显示
} catch (Exception e) {
Log.e("error",e.getMessage());
}
}
}
super.onCallStateChanged(state, incomingNumber);
}
}
}

android怎么实现自动接听和挂断电话功能

标签:

原文地址:http://www.cnblogs.com/flyoung/p/4922886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!