思路:
?
?对来电进行监控
1)对于通讯录的电话来电不做监控
2)对于通讯录里没有的电话号码进行监控
监控规则:
? 1)正则匹配 如:杭州的本地号码需要接听 其他地区来电拒接
? 2)如果一些确认需要来电的号码但又不想加入黑名单的号码 加入白名单管理列表;
? 3)如果是手机来电,拒接的发送一条短信(询问是何人找我干什么事情?避免误伤),如果是座机就误伤吧!
?
? android 官方教程http://developer.android.com/training/basics/firstapp/building-ui.html
https://gist.github.com/CyanogenMod/android_frameworks_base/tree/cf4550c3198d6b3d92cdc52707fe70d7cc0caa9f
—————————————分隔线——————————
?
下面是收集的一些代码片段:
public class PhoneStatReceiver extends BroadcastReceiver{
???????
??????? private static final String TAG = "PhoneStatReceiver";
???????
//??????? private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();
???????
??????? private static boolean incomingFlag = false;
???????
??????? private static String incoming_number = null;
??????? @Override
??????? public void onReceive(Context context, Intent intent) {
??????????????? //如果是拨打电话
??? ??? ??? ??? if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){???????????????????????
??????????????????????? incomingFlag = false;
??????????????????????? String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);???????
??????????????????????? Log.i(TAG, "call OUT:"+phoneNumber);???????????????????????
??????????????? }else{???? //action.equals(MyBroadcastReceiver.B_PHONE_STATE)??????????????????
??????????????????????? //如果是来电
??????????????????????? TelephonyManager tm =
??????????????????????????? (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);???????????????????????
???????????????????????
??????????????????????? switch (tm.getCallState()) {
??? ??? ??? ??? ??? ??? ??? case TelephonyManager.CALL_STATE_RINGING:
??????????????????????????????? incomingFlag = true;//标识当前是来电
??????????????????????????????? incoming_number = intent.getStringExtra("incoming_number");
???????????????????????????????
??? ??? ??? ??? ??? ??? ??? ??? if (isBlock(incoming_number)) {?
??? ??? ??? ??? ??? ??? ??? ??? ??? try {?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? Method method = Class.forName(?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? "android.os.ServiceManager").getMethod(?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? "getService", String.class);?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? // 获取远程TELEPHONY_SERVICE的IBinder对象的代理?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? IBinder binder = (IBinder) method.invoke(null,?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? new Object[] { TELEPHONY_SERVICE });?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? // 将IBinder对象的代理转换为ITelephony对象?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ITelephony telephony = ITelephony.Stub?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? .asInterface(binder);?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? // 挂断电话?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? telephony.endCall();?
??? ??? ??? ??? ??? ??? ??? ??? ??? } catch (Exception e) {?
??? ??? ??? ??? ??? ??? ??? ??? ??? ??? e.printStackTrace();?
??? ??? ??? ??? ??? ??? ??? ??? ??? }?
??? ??? ??? ??? ??? ??? ??? ??? }?
??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? ??? ???
??? ??? ??? ??? ??? ??? ??? ??? Log.i(TAG, "RINGING :"+ incoming_number);
??????????????????????????????? break;
??????????????????????? case TelephonyManager.CALL_STATE_OFFHOOK:???????????????????????????????
??????????????????????????????? if(incomingFlag){
??????????????????????????????????????? Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
??????????????????????????????? }
??????????????????????????????? break;
???????????????????????
??????????????????????? case TelephonyManager.CALL_STATE_IDLE:???????????????????????????????
??????????????????????????????? if(incomingFlag){
??????????????????????????????????????? Log.i(TAG, "incoming IDLE");???????????????????????????????
??????????????????????????????? }
??????????????????????????????? break;
??????????????????????? }
??????????????? }
??????? }
}
在AndroidManifest.xml,配置写好的Receiver,并拦截相应的BroadCastAction,
另外注意加上相应的权限。
<receiver android:name=".filter.PhoneStatReceiver">?
??????????? <intent-filter>
???????????????? <action android:name="android.intent.action.PHONE_STATE"/>??????????
???????????????? <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
??????????? </intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_CONTACTS"/>?
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
??? ??? 来电分三种状态:
CALL_STATE_RINGING:来电响铃
CALL_STATE_OFFHOOK:摘机
CALL_STATE_IDLE:挂机
http://blog.csdn.net/lee576/article/details/8053387
http://wenku.baidu.com/view/f391162e453610661ed9f4b1.html
???? private static final String[] PHONES_PROJECTION = new String[] {
??????????? Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID };
??? /**得到手机通讯录联系人信息**/
??? private void getPhoneContacts() {
??????? ContentResolver resolver = mContext.getContentResolver();
??????? // 获取手机联系人
??????? Cursor phoneCursor = resolver.query(Phone.CONTENT_URI,PHONES_PROJECTION, null, null, null);
??????? if (phoneCursor != null) {
??????????? while (phoneCursor.moveToNext()) {
??????????????? //得到手机号码
??????????????? String phoneNumber = phoneCursor.getString(PHONES_NUMBER_INDEX);
??????????????? //当手机号码为空的或者为空字段 跳过当前循环
??????????????? if (TextUtils.isEmpty(phoneNumber))
??????????????????? continue;
???????????????
??????????????? //得到联系人名称
??????????????? String contactName = phoneCursor.getString(PHONES_DISPLAY_NAME_INDEX);
???????????????
??????????????? //得到联系人ID
??????????????? Long contactid = phoneCursor.getLong(PHONES_CONTACT_ID_INDEX);
??????????????? //得到联系人头像ID
??????????????? Long photoid = phoneCursor.getLong(PHONES_PHOTO_ID_INDEX);
???????????????
??????????????? //得到联系人头像Bitamp
??????????????? Bitmap contactPhoto = null;
??????????????? //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的
??????????????? if(photoid > 0 ) {
??????????????????? Uri uri =ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,contactid);
??????????????????? InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);
??????????????????? contactPhoto = BitmapFactory.decodeStream(input);
??????????????? }else {
??????????????????? contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);
??????????????? }
???????????????
??????????????? mContactsName.add(contactName);
??????????????? mContactsNumber.add(phoneNumber);
??????????????? mContactsPhonto.add(contactPhoto);
??????????? }
??????????? phoneCursor.close();
??????? }
??? }
??? private Cursor getContacts(){?
??????????? // Run query?
??????????? Uri uri = ContactsContract.Contacts.CONTENT_URI;?
??????????? String[] projection = new String[] {?
??????????????????? ContactsContract.Contacts._ID,?
??????????????????? ContactsContract.Contacts.DISPLAY_NAME?
??????????? };?
??????????? String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +?
??????????????????? (mShowInvisible ? "0" : "1") + "'";?
??????????? String[] selectionArgs = null;?
??????????? //sort ordering based on localized preferences(sqlite排序用法:根据本地化设置对字符串进行比较排序)?
??????????? String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";?
?????
??????????? return managedQuery(uri, projection, selection, selectionArgs, sortOrder);?
??????? }?