添加权限
<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 实现自动接听和挂断电话功能的更多相关文章

  1. android 自动拨打电话 挂断电话代码

    页面布局文件代码  (  res下面的layout下面的activity_main.xml代码 ) <RelativeLayout xmlns:android="http://sche ...

  2. Android自动接听&挂断电话(包含怎么应对4.1以上版本的权限检

    一  前言 这两天要研究类似白名单黑名单以及手势自动接听的一些功能,所以呢,自然而然的涉及到怎么自动接听/挂断电话的功能了.对于自动接听这一块,android4.1版本及其以上的版本和之前的版本处理逻 ...

  3. Android之——自己主动挂断电话的实现

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

  4. android 接听和挂断实现方式

    参考:android 来电接听和挂断 支持目前所有版本 注意:android2.3版本及以上不支持下面的自动接听方法. (会抛异常:java.lang.SecurityException: Neith ...

  5. Android 电话自己主动接听和挂断具体解释

    1.通过aidl及反射实现挂断电话 详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件 ...

  6. Android接听、挂断电话

    新建一个名为ITelephony的aidl文件,注意包名不能改变,因为是通过反射方式来实现接听和挂断的

  7. eclipse下Android无法自动生成apk文件怎么办?

    eclipse下Android无法自动生成apk文件怎么办? 现象:创建android工程后,通过手动build/clean或自动build均无法在bin文件夹下生成.apk文件 解决方法:进入win ...

  8. Android:TextView 自动滚动(跑马灯) (转)

    Android:TextView 自动滚动(跑马灯)       TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true ...

  9. Android 源代码自动编译packages/apps

    /*************************************************************************** * Android 源代码自动编译packag ...

随机推荐

  1. 部署测试环境(ubuntu+mysql+tomcat)

    背景:入职新公司,广州这边没有测试开发环境,需要自己搭建一个:要求ubuntu+mysql+tomcat有具体版本要求:   2015/4/13 下载Ubuntu12.04 http://mirror ...

  2. windows下mysql5.7安装及配置

    装完msi后,复制my-default.ini文件,黏贴为my.ini文件,内容修改如下: # For advice on how to change settings please see# htt ...

  3. html --- VML --- javascript --- 旋转矩形

    矢量标记语言 --- Vector Markup Language 运行它的代码需要打开IE的兼容性视图 如有疑问请参考:http://msdn.microsoft.com/en-us/library ...

  4. 比赛组队问题 --- 递归解法 --- java代码 --- 八皇后问题

    两队比赛,甲队为A.B.C3人,乙队为X.Y.Z3人.已知A不和X比,C不和X.Z比,请编程序找出3队赛手名单 采用了与八皇后问题相似的解法,代码如下: 如有疑问请链接八皇后问题的解法:http:// ...

  5. 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer

    12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...

  6. 移动开发必备!15款jQuery Mobile插件

    移动互联网的发展,来自PC端的网页并不能完全自适应移动端页面需求,使得响应式设计体验产生并成为潮流,也正是这样一种需求,促成了jQuery Mobile的流行.jQuery Mobile这样一款基于j ...

  7. jQuery Mobile 页面事件总结

    一.页面初始化事件(Page initiallization) 在页面创建前,当页面创建时,以及在页面初始化之后.只在第一次加载时执行. 1. pagebeforecreate 页面创建前 [sour ...

  8. LeetCode(5) - Longest Palindromic Substring

    这道题要求的是给你一个string, 如“adcdabcdcba",要求返回长度最大的回文子字符串.这里有两个条件,一是子字符串,而是回文.用纯暴力搜索的话,需要用到O(n^3)的时间,必然 ...

  9. spark的环境安装

    1.安装sbt 正常安装流程. 在cmd里运行的时候,要提前设置代理(如果上网有代理),set JAVA_OPTS=-Dhttp.proxySet=true -Dhttp.proxyHost=172. ...

  10. CodeForces 622 A.Infinite Sequence

    A.Infinite Sequence time limit per test 1 second memory limit per test 256 megabytes input standard ...