首页 > 其他 > 详细

如何截取指定号码的短信,并且不让系统截取到通知用户

时间:2014-03-22 17:50:38      阅读:379      评论:0      收藏:0      [点我收藏+]

如何截取指定号码的短信,并且不让系统截取到通知用户

    以下是别人使用 ContentObserver 来监听短信数据库变化来实现的。 

Java代码   bubuko.com,布布扣
  1. public class ScreenTest extends Activity {  
  2.   
  3.   class SmsContent extends ContentObserver{  
  4.   
  5.   private Cursor cursor = null;  
  6.   
  7.   public SmsContent(Handler handler) {  
  8.   
  9.   super(handler);  
  10.   
  11.   }  
  12.   
  13.   /** 
  14.  
  15.   * @Description 当短信表发送改变时,调用该方法 
  16.  
  17.   * 需要两种权限 
  18.  
  19.   * android.permission.READ_SMS 读取短信 
  20.  
  21.   * android.permission.WRITE_SMS 写短信 
  22.  
  23.   * @Author Snake 
  24.  
  25.   * @Date 2010-1-12 
  26.  
  27.   */  
  28.   
  29.   @Override  
  30.   
  31.   public void onChange(boolean selfChange) {  
  32.   
  33.   // TODO Auto-generated method stub  
  34.   
  35.   super.onChange(selfChange);  
  36.   
  37.   //读取收件箱中指定号码的短信  
  38.   
  39.   cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id""address""read"}, " address=? and read=?"new String[]{"12345678901""0"}, "date desc");  
  40.   
  41.   if (cursor != null){  
  42.   
  43.   ContentValues values = new ContentValues();  
  44.   
  45.   values.put("read""1"); //修改短信为已读模式  
  46.   
  47.   cursor.moveToFirst();  
  48.   
  49.   while (cursor.isLast()){  
  50.   
  51.   //更新当前未读短信状态为已读  
  52.   
  53.   getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?"new String[]{""+cursor.getInt(0)});  
  54.   
  55.   cursor.moveToNext();  
  56.   
  57.   }  
  58.   
  59.   }  
  60.   
  61.   }  
  62.   
  63.   }  
  64.   
  65.   /** Called when the activity is first created. */  
  66.   
  67.   @Override  
  68.   
  69.   public void onCreate(Bundle savedInstanceState) {  
  70.   
  71.   super.onCreate(savedInstanceState);  
  72.   
  73.   setContentView(R.layout.main);  
  74.   
  75.   SmsContent content = new SmsContent(new Handler());  
  76.   
  77.   //注册短信变化监听  
  78.   
  79.   this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"),   
  80.   
  81.   
  82. true, content);  
  83.   
  84.   }  
  85.   
  86.   }  
  87.   
  88.   public class ScreenTest extends Activity {  
  89.   
  90.   class SmsContent extends ContentObserver{  
  91.   
  92.   private Cursor cursor = null;  
  93.   
  94.   public SmsContent(Handler handler) {  
  95.   
  96.   super(handler);  
  97.   
  98.   }  
  99.   
  100.   /** 
  101.  
  102.   * @Description 当短信表发送改变时,调用该方法 
  103.  
  104.   * 需要两种权限 
  105.  
  106.   * android.permission.READ_SMS读取短信 
  107.  
  108.   * android.permission.WRITE_SMS写短信 
  109.  
  110.   * @Author Snake 
  111.  
  112.   * @Date 2010-1-12 
  113.  
  114.   */  
  115.   
  116.   @Override  
  117.   
  118.   public void onChange(boolean selfChange) {  
  119.   
  120.   // TODO Auto-generated method stub  
  121.   
  122.   super.onChange(selfChange);  
  123.   
  124.   //读取收件箱中指定号码的短信  
  125.   
  126.   cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id""address""read"}, " address=? and read=?"new String[]{"12345678901""0"}, "date desc");  
  127.   
  128.   if (cursor != null){  
  129.   
  130.   ContentValues values = new ContentValues();  
  131.   
  132.   values.put("read""1"); //修改短信为已读模式  
  133.   
  134.   cursor.moveToFirst();  
  135.   
  136.   while (cursor.isLast()){  
  137.   
  138.   //更新当前未读短信状态为已读  
  139.   
  140.   getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?"new String[]{""+cursor.getInt(0)});  
  141.   
  142.   cursor.moveToNext();  
  143.   
  144.   }  
  145.   
  146.   }  
  147.   
  148.   }  
  149.   
  150.   }  
  151.   
  152.   /** Called when the activity is first created. */  
  153.   
  154.   @Override  
  155.   
  156.   public void onCreate(Bundle savedInstanceState) {  
  157.   
  158.   super.onCreate(savedInstanceState);  
  159.   
  160.   setContentView(R.layout.main);  
  161.   
  162.   SmsContent content = new SmsContent(new Handler());  
  163.   
  164.   //注册短信变化监听  
  165.   
  166.   this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);  
  167.   
  168.   }  
  169.   
  170.   }  

如何截取指定号码的短信,并且不让系统截取到通知用户,布布扣,bubuko.com

如何截取指定号码的短信,并且不让系统截取到通知用户

原文:http://www.cnblogs.com/lining301/p/3617681.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!