我的手机管家(21) 设置中心 :
包括三个界面:四个功能
(1)电话归属地查询
使用SQLIteDatabase 查询address.db, 查询指定号码的归属地
(2) 检查应用版本更新
(3)关于我的手机管家信息
(4)黑名单服务的开启与关闭
黑名单服务:
通过绑定广播进行短信的拦截, 会比静态注册先拦截短信
通过电话管理器TelephonyManager监听电话状态
一旦有电话打入, 首先进行黑名单对比。
package com.chb.myphonesave.service; import java.lang.reflect.Method; 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.SmsMessage; import android.telephony.TelephonyManager; import com.android.internal.telephony.ITelephony; import com.chb.myphonesave.dao.BlackNumberDao; /** * */ public class BlackNumberService extends Service { private InnerSmsReceiver mReceiver; private TelephonyManager mTM; private MyPhoneStateListener mListener; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Context ctx = getApplicationContext(); mTM = (TelephonyManager) ctx.getSystemService(TELEPHONY_SERVICE); mListener = new MyPhoneStateListener(); mTM.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE); // 注册短信广播监听 mReceiver = new InnerSmsReceiver(); IntentFilter filter = new IntentFilter( "android.provider.Telephony.SMS_RECEIVED"); filter.setPriority(Integer.MAX_VALUE);// 设置优先级 registerReceiver(mReceiver, filter); } class MyPhoneStateListener extends PhoneStateListener { public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING: int exist = BlackNumberDao.getInstance( BlackNumberService.this).find(incomingNumber); if (exist > 0) { // 1, 3 int mode = BlackNumberDao.getInstance( BlackNumberService.this).findMode(incomingNumber); if (mode == 1 || mode == 3) {// 拦截电话 // endCall endCall(); } } break; default: break; } super.onCallStateChanged(state, incomingNumber); } } public void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); mReceiver = null; // 停止来电 监听 mTM.listen(mListener, PhoneStateListener.LISTEN_NONE); mListener = null; } /** * 挂断电话 需要权限: <uses-permission * android:name="android.permission.CALL_PHONE"/> */ public void endCall() { // android.os.ServiceManager // ITelephony.Stub.asInterface(ServiceManager.getService(Context.TELEPHONY_SERVICE)); try { Class clazz = BlackNumberService.class.getClassLoader().loadClass( "android.os.ServiceManager");// 获取ServiceManager的字节码 Method method = clazz.getDeclaredMethod("getService", String.class);// 获取方法 IBinder binder = (IBinder) method.invoke(null, Context.TELEPHONY_SERVICE);// 通过反射调用方法,获取IBinder代理对象 ITelephony telephony = ITelephony.Stub.asInterface(binder); telephony.endCall();// 挂断电话 } catch (Exception e) { e.printStackTrace(); } } // 动态注册的广播比静态注册的更先拦截到短信 class InnerSmsReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Object[] objs = (Object[]) intent.getExtras().get("pdus"); for (Object object : objs) {// 140个字节, 70个汉字, // 当短信超过140个字节时,会分多条短信发送,所以会循环多次, // 一般情况下该for循环只走一次 SmsMessage message = SmsMessage.createFromPdu((byte[]) object); String phoneNumber = message.getOriginatingAddress();// 获取短信来电号码 int exist = BlackNumberDao.getInstance( BlackNumberService.this).find(phoneNumber); if (exist > 0) {// 如果号码在黑名单范围之内 int mode = BlackNumberDao.getInstance( BlackNumberService.this).findMode(phoneNumber); if (mode > 1) {// 如果发现是拦截短信或者短信+电话 abortBroadcast();// 终止广播传递 } } } } } }
时间: 2024-11-03 21:58:04