添加权限
<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);
}
}
}

最新文章

  1. iOS app上架需要提前准备的东西
  2. JavaScript之数组方法整理
  3. 在Ubuntu上安装Hadoop(单机模式)步骤
  4. NSFileManager(沙盒文件管理)数据持久化 &lt;序列化与反序列化&gt;
  5. 【iCore3 双核心板_ uC/OS-III】例程五:软件定时器
  6. retrofit2 上传图片
  7. 将Session写入数据库
  8. 使用Expression做Linq的參數化排序
  9. struts文件上传(单文件)
  10. CSS 强制换行和禁止换行学习
  11. javaweb学习总结(四十七)——监听器(Listener)在开发中的应用
  12. Delphi 动态改变Rzsplitter的Orientation(方向)属性
  13. springMVC3学习(八)--全局的异常处理
  14. 半同步半异步模式的实现 - MSMQ实现
  15. LayoutInflater 类的使用
  16. Spring基础知识之依赖注入
  17. Python之路1-变量、数据类型、循环语法
  18. Android+openCV 的坑
  19. IIS + FastCGI+php(从5.2升级到5.3)
  20. Docker 查看容器 IP 地址

热门文章

  1. VC远控(三)磁盘显示
  2. C# 类和结构
  3. 【2013微软面试题】输出节点数为n的二叉树的所有形态
  4. Linux中的.emacs文件
  5. Windows8.1 安装office2013并激活
  6. SpringMVC + Spring + MyBatis 学习笔记:提交数据遭遇基础类型和日期类型报400错误解决方法
  7. CentOS6.5修改mysql数据文件路径
  8. tmux的使用方法和个性化配置
  9. MVC Dynamic Authorization--示例市在Action前进行的验证,应提前到Auth过滤器
  10. 构建Spark作业