码迷,mamicode.com
首页 > 其他好文 > 详细

26.监听通知记录、观察者

时间:2015-11-25 10:59:21      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

  • 黑名单监听通话记录,使黑名单里的电话不能打进来,也不能打出去
  • 在打电话的时候有时候能把通话记录的黑名单删除,有时候删不了,这是由于是俩个进程,所以注册个观察者监听通话记录
  • 在找内容提供者的主机名时先看清单文件
CallSmsSafeService 
  1. public class CallSmsSafeService extends Service {
  2. private InnerSmsReceiver receiver;
  3. private BlackNumberDao dao;
  4. //监听当前呼叫的状态
  5. private TelephonyManager tm;
  6. private MyPhoneListener listener;
  7. @Override
  8. public IBinder onBind(Intent intent) {
  9. return null;
  10. }
  11. private class InnerSmsReceiver extends BroadcastReceiver{
  12. @Override
  13. public void onReceive(Context context, Intent intent) {
  14. System.out.println("内部广播接收者短信到来啦。。。");
  15. //检查发件人是否是黑名单号码,设置短信拦截全部拦截
  16. Object[] objs = (Object[]) intent.getExtras().get("pdus");
  17. for (Object obj : objs) {
  18. SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) obj);
  19. String sender = smsMessage.getOriginatingAddress();//得到短信发件人
  20. String result = dao.findMode(sender);
  21. if("2".equals(result) || "3".equals(result)){
  22. abortBroadcast();//拦截短信
  23. }
  24. String body = smsMessage.getDisplayMessageBody();
  25. if(body.contains("fapiao")){
  26. abortBroadcast();
  27. }
  28. }
  29. }
  30. }
  31. @Override
  32. public void onCreate() {
  33. tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  34. listener = new MyPhoneListener();
  35. tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);//监听电话的状态
  36. dao = new BlackNumberDao(this);
  37. receiver = new InnerSmsReceiver();
  38. IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
  39. filter.setPriority(Integer.MAX_VALUE);
  40. registerReceiver(receiver,filter);
  41. super.onCreate();
  42. }
  43. private class MyPhoneListener extends PhoneStateListener{
  44. @Override
  45. public void onCallStateChanged(int state, String incomingNumber) {
  46. super.onCallStateChanged(state, incomingNumber);
  47. switch (state) {
  48. case TelephonyManager.CALL_STATE_RINGING: //电话响铃的状态
  49. String mode = dao.findMode(incomingNumber);
  50. if("1".equals(mode) || "3".equals(mode)){
  51. System.out.println("这是黑名单的电话 "+incomingNumber);
  52. //删除呼叫记录
  53. //另外一个应用程序联系人的应用的私有数据库
  54. // deleteCallLog(incomingNumber);
  55. //观察呼叫记录数据库内容的变化
  56. Uri uri = Uri.parse("content://call_log/calls");
  57. getContentResolver().registerContentObserver(uri,true,new CallLogObserver(incomingNumber,new Handler()));
  58. endcall();//挂断电话 另外一个进程里面运行的 远程服务的方法。方法调用后,呼叫记录可能还没有生产。
  59. }
  60. break;
  61. }
  62. }
  63. }
  64. /**
  65. * 监听通话记录
  66. */
  67. private class CallLogObserver extends ContentObserver{
  68. private String incomingNumber;
  69. public CallLogObserver(String incomingNumber,Handler handler) {
  70. super(handler);
  71. this.incomingNumber = incomingNumber;
  72. }
  73. @Override
  74. public void onChange(boolean selfChange) {
  75. System.out.println("数据库产生了呼叫记录");
  76. getContentResolver().unregisterContentObserver(this);
  77. deleteCallLog(incomingNumber);
  78. super.onChange(selfChange);
  79. }
  80. }
  81. @Override
  82. public void onDestroy() {
  83. unregisterReceiver(receiver);
  84. receiver = null;
  85. tm.listen(listener,PhoneStateListener.LISTEN_NONE);
  86. listener = null;
  87. super.onDestroy();
  88. }
  89. /**
  90. * 利用内容提供者删除呼叫记录
  91. * @param incomingNumber
  92. */
  93. public void deleteCallLog(String incomingNumber) {
  94. ContentResolver resolver = getContentResolver();
  95. //呼叫记录uri的路径
  96. Uri uri = Uri.parse("content://call_log/calls");
  97. System.out.println("删除 "+incomingNumber);
  98. // CallLog.CONTENT_URI;
  99. resolver.delete(uri,"number=?",new String[]{incomingNumber});
  100. }
  101. /**
  102. * 挂断电话
  103. */
  104. public void endcall() {
  105. try {
  106. Class<?> clazz = CallSmsSafeService.class.getClassLoader().loadClass("android.os.ServiceManager");
  107. Method method = clazz.getDeclaredMethod("getService",String.class);
  108. IBinder b = (IBinder) method.invoke(null,TELEPHONY_SERVICE);
  109. //获取到了原生未经修改包装的系统电话的管理服务
  110. ITelephony iTelephony = ITelephony.Stub.asInterface(b);
  111. iTelephony.endCall();//挂断电话
  112. } catch (Exception e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. }

 




26.监听通知记录、观察者

标签:

原文地址:http://www.cnblogs.com/liuyu0529/p/4993782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!