Android 自动接听电话和挂断
来源:互联网 发布:云南广电网络营业厅 编辑:程序博客网 时间:2024/04/28 15:37
注意:android2.3版本及以上不支持下面的自动接听方法。(会抛异常:java.lang.SecurityException: Neither user xxxxx nor current process has android.permission.MODIFY_PHONE_STATE.)
原因:android2.3版本及以上android.permission.MODIFY_PHONE_STATE权限限制已经改为系统权限。
1、把编译好的apk放入sd卡,2、用root explorer复制到/system/app目录下,3、修改apk权限rw-r--r--,关机,重启,测试一下,成功! 注意:只能在root的手机上使用
使用映射机制实现自动接听和挂断:
第一步:准备应用环境需要的系统包和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.moho.telephone.utils;import java.lang.reflect.Method;import com.android.internal.telephony.ITelephony;import android.telephony.TelephonyManager;/**
* moho
*/public class PhoneUtils {public static ITelephony getITelephony(TelephonyManager telephony) throws Exception {Method getITelephonyMethod = telephony.getClass().getDeclaredMethod("getITelephony");getITelephonyMethod.setAccessible(true);//私有化函数也能使用return (ITelephony)getITelephonyMethod.invoke(telephony);}}
MyPhoneBroadcastReceiver
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;@Overridepublic 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);}}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
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;@Overridepublic 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配置权限
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
因为Android2.3以上增加了对permission android.permission.MODIFY_PHONE_STATE 的限制,2.3之前的通过反射机制调用ITelephone的能力的做法已经不适用。
2.3上实现方式:
public synchronized void answerRingingCall() {//查询系统PhoneAPP应用(PhoneGlobals.java)实现了对耳机插入、多媒体按键等通知的接受和处理。其中未发现有特殊的地方,个人认为,如果系统接收到此广播应该可以///进行接听或挂断操作。 private void answerRingingCallWithBroadcast(Context context,TelephonyManager telmanager){ 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); } }
效果图:
工程项目:http://download.csdn.net/detail/weiyirong/6872889
项目已开源:https://github.com/weiyirong/TelephoneAutoController
- Android 自动接听电话和挂断
- Android挂断和接听电话
- [Android]挂断、接听电话
- [Android]挂断、接听电话
- [Android]挂断、接听电话
- [Android]挂断、接听电话
- [应用代码] android 自动接听电话和挂断 (适合目前所有版本)
- Android(AIDL)自动重复拨号及挂断/接听电话
- Android 自动接听电话
- Android 4.0自动接听电话
- android 5.0 自动接听电话
- android 5.0 自动接听电话
- android 来电自动接听和自动挂断
- android 来电自动接听和自动挂断
- android 来电自动接听和自动挂断
- android自动接听和挂断电话
- Android 电话自动接听和挂断详解
- android 自动挂断来电
- uplodify 结合 imgareaselect 实现头像截图上传
- Unity 3D + Vuforia制作AR人物互动
- OpenGL学习三十二:二次几何体
- 下面的一小段儿代码,我不知道为什么它的执行逻辑是什么?
- 我的vim在windows下的配置
- Android 自动接听电话和挂断
- 无序双向链表的优先队列
- struts2中的jar文件
- 机器学习是什么
- 织梦通过 phpmyadmin 导出的数据,再次导入的时候报错
- JAVA基础——包精简总结
- MIPS系列笔记-CP0 Hazard
- Bootstrap多级级联菜单
- IOS Sent Events 事件