Android 对电话进行监听和挂断
1.添加权限
<!--拨打电话的权限-->
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--电话拦截-->
<receiver android:name=".receiver.PhoneBroadcastReceiver">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<!-- 电话拦截服务 --> <service android:name="com.lvshandian.menshen.service.PhoneService">
<intent-filter>
<action android:name="com.xinwang.telesms.PhoneReciever"></action>
<action android:name="com.xinwang.telesms.server.IMICHAT" />
</intent-filter>
</service>
package com.lvshandian.menshen.receiver; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;
import android.util.Log; import com.android.internal.telephony.ITelephony;
import com.lvshandian.menshen.service.PhoneService; import java.lang.reflect.Method;
import java.util.ArrayList; /**
* Created by zhang on 2016/11/3.
* 创建电话的监听
*/ public class PhoneBroadcastReceiver extends BroadcastReceiver { String TAG = "tag";
TelephonyManager telMgr; @Override
public void onReceive(Context context, Intent intent) { telMgr = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
switch (telMgr.getCallState()) {
//来电
case TelephonyManager.CALL_STATE_RINGING:
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v(TAG, "number:" + number);
Intent myintent = new Intent(context, PhoneService.class);
myintent.setAction("com.lvshandian.menshen.service.PhoneReciever");
context.startService(myintent);
// if (!getPhoneNum(context).contains(number)) {
// SharedPreferences phonenumSP = context.getSharedPreferences("in_phone_num", Context.MODE_PRIVATE);
// SharedPreferences.Editor editor = phonenumSP.edit();
// editor.putString(number, number);
// editor.commit();
// endCall();
// }
break;
//响铃
case TelephonyManager.CALL_STATE_OFFHOOK:
break;
//挂断
case TelephonyManager.CALL_STATE_IDLE:
break;
} } /**
* 挂断电话
*/
private void endCall() {
Class<TelephonyManager> c = TelephonyManager.class;
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
Log.e(TAG, "End call.");
iTelephony = (ITelephony) getITelephonyMethod.invoke(telMgr, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
Log.e(TAG, "Fail to answer ring call.", e);
}
} private ArrayList<String> getPhoneNum(Context context) {
ArrayList<String> numList = new ArrayList<String>();
//得到ContentResolver对象
ContentResolver cr = context.getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
// 取得联系人ID
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
// 取得电话号码(可能存在多个号码)
while (phone.moveToNext()) {
String strPhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numList.add(strPhoneNumber);
Log.v("tag", "strPhoneNumber:" + strPhoneNumber);
} phone.close();
}
cursor.close();
return numList;
} }
//进行过滤对比电话
package com.lvshandian.menshen.service; import java.lang.reflect.Method;
import java.util.List; import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager; import com.android.internal.telephony.ITelephony;
import com.lvshandian.menshen.bean.PhoneBean;
import com.lvshandian.menshen.receiver.PhoneBroadcastReceiver;
import com.lvshandian.menshen.utils.TextUtils;
import com.lvshandian.menshen.utils.XUtils; public class PhoneService extends Service {
String TAG = "tag";
TelephonyManager telManager; @Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
} @Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(new MyPhoneStateListener(),
PhoneStateListener.LISTEN_CALL_STATE);
} /**
* 挂断电话
*/
private void endCall() {
Class<TelephonyManager> c = TelephonyManager.class;
try {
Method getITelephonyMethod = c.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
ITelephony iTelephony = null;
iTelephony = (ITelephony) getITelephonyMethod.invoke(telManager, (Object[]) null);
iTelephony.endCall();
} catch (Exception e) {
}
} private class MyPhoneStateListener extends PhoneStateListener {
String phoneNumber; public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: /* 接通 */
phoneNumber = incomingNumber;
List<PhoneBean> list = XUtils.findAll(PhoneBean.class);
for (int i = 0; i < list.size(); i++) {
if (TextUtils.isString(list.get(i).getDnseg(), phoneNumber)) {
endCall();
break;
}
} }
super.onCallStateChanged(state, incomingNumber);
} } @Override
public void onDestroy() {
super.onDestroy();
Intent localIntent = new Intent();
localIntent.setClass(this, PhoneService.class); // 销毁时重新启动Service
this.startService(localIntent);
} private int flags; @Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY_COMPATIBILITY;
} @Override
public void onStart(Intent intent, int startId) {
// 再次动态注册广播
IntentFilter localIntentFilter = new IntentFilter("android.intent.action.USER_PRESENT");
localIntentFilter.setPriority(Integer.MAX_VALUE);// 整形最大值
myReceiver searchReceiver = new myReceiver();
registerReceiver(searchReceiver, localIntentFilter);
super.onStart(intent, startId);
} public class myReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, PhoneBroadcastReceiver.class));
}
}
}
Android 对电话进行监听和挂断的更多相关文章
- android 呼入电话的监听(来电监听)转
需要权限: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 方式一:通过广 ...
- Android EditText截获与监听输入事件
Android EditText截获与监听输入事件共有2种方法: 1.第一种方法:使用setOnKeyListener(),不过这种方式只能监听硬键盘事件. edittext.setOnKeyLi ...
- Android Back Home键监听
Android Back Home键监听 Back键的监听 对于Back键的监听比较容易,可以在多个系统回调处拦截,比如在activity的下列方法中都可以收到Back键按下的事件: @Overrid ...
- Android对ScrollView滚动监听,实现美团、大众点评的购买悬浮效果
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17761431),请尊重他人的辛勤劳动成果,谢谢! 我之前写 ...
- Android 电话自己主动接听和挂断具体解释
1.通过aidl及反射实现挂断电话 详细分三步: (1)ITelephony.aidl ,必须新建com.android.internal.telephony包并放入ITelephony.aidl文件 ...
- 电话状态监听 - iOS
今天接到一个监听状态的需求,当使用 App 时若电话介入需要对当前状态进行监听操作(注:并非通话内容),根据不同的状态实行相关的需求操作,废话不多说步骤如下. 首先,常规操作先引用对应的头文件,来为后 ...
- Android addTextChangedListener(文本监听)参数解释及实现EditText字数监听
由于最近做项目要检测EditText中输入的字数长度,从而接触到了Android中EditText的监听接口,TextWatcher.它有三个成员方法,第一个after很简单,这个方法就是在EditT ...
- Android来电、去电监听
Android手机中添加手机来电的状态,使用PhoneStateListener来监听. TelephonyManager telephonyManager = (TelephonyManager) ...
- iOS实现电话状态监听 CoreTelephony
在程序中如果需要监听电话状态,可以引入CoreTelephony框架,这个框架包含了电话相关的API,可以实现监测来电,查看运营商信息等功能.下面就是具体的实现监测来电的代码.一定要把center写成 ...
随机推荐
- JavaScript 类式继承与原型继承
交叉着写Java和Javascript都有2年多了,今天来总结下自己所了解的Javascript类与继承. Javascript本身没有类似Java的面向对象的类与继承术语,但其基于原型对象的思想却可 ...
- dubbo 试用全过程
概述: dubbo服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只 ...
- Nginx配置(日志服务器中关于日志的产生)
一:概括 1.需要配置的概括 定义日志格式 日志的分割字段:^A 日志格式:IP地址^A服务器时间^A请求参数 配置location,记录请求日志到本地磁盘 将数据按照给定的日志格式存储到本地磁盘 二 ...
- python 常用模块 Top200
名次 模块名称 被使用项目数 1 sys 7858 2 os 6983 3 re 5663 4 time 5268 5 random 3339 6 datetime 3310 7 setuptools ...
- c#中浅拷贝和深拷贝的理解
c#中拷贝有浅拷贝和深拷贝之分. 例如对象A,其中有值类型字段和引用类型字段: 1.浅拷贝: 对于值类型字段,直接逐位复制到新拷贝的副本对象中,修改副本的字段的值,不会影响源对象中字段的值: 对于引用 ...
- Android 自定义RecyclerView 实现真正的Gallery效果
http://blog.csdn.net/lmj623565791/article/details/38173061
- Elasticsearch + logstash中文指南
http://kibana.logstash.es/content/logstash/examples/nginx-access.html http://es.xiaoleilu.com/030_Da ...
- AngularJS Best Practices: ASP.NET MVC Directory Structure
/Content----- images/ // Images for your app----- css/ // Styles for your app/Scripts----- libs/ // ...
- RDIFramework.NET ━ 9.3 用户管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.3 用户管理 -Web部分 用户管理模块主要是对可登录系统的用户进行管理.后续的工作如:用户归属角色.权限的分配.用户所拥有 ...
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)
控制台程序. 在Sketcher中创建形状时,并不知道应该以什么顺序创建不同类型的形状,这完全取决于使用Sketcher程序生成草图的人.因此需要绘制形状,对它们执行其他操作而不必知道图形是什么.当然 ...