使用ContentObserver监听短信数据库的变化

本文介绍了一种使用Android短信数据库监听实现短信状态更新的方法,包括如何注册短信变化监听和更新短信为已读模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/qinjuning/article/details/7047607

    public class ScreenTest extends Activity {  
        class SmsContent extends ContentObserver {  
            private Cursor cursor = null;  
            public SmsContent(Handler handler) {  
                super(handler);  
            }  

            /** 
             * @Description 当短信表发送改变时,调用该方法 
             * 需要两种权限 
             * android.permission.READ_SMS 读取短信 
             * android.permission.WRITE_SMS 写短信 
             * @Author Snake 
             * @Date 2010-1-12 
             */  
            @Override  
            public void onChange(boolean selfChange) {  
                // TODO Auto-generated method stub  
                super.onChange(selfChange);  
                // 读取收件箱中指定号码的短信  
                cursor = managedQuery(Uri.parse("content://sms/inbox"), new String[]{"_id", "address", "read"}, " address=? and read=?", new String[]{"12345678901", "0"}, "date desc");  
                if (cursor != null) {  
                    ContentValues values = new ContentValues();  
                    values.put("read", "1"); //修改短信为已读模式  
                    cursor.moveToFirst();  
                    while (cursor.isLast()){  
                        //更新当前未读短信状态为已读  
                        getContentResolver().update(Uri.parse("content://sms/inbox"), values, " _id=?", new String[]{""+cursor.getInt(0)});  
                        cursor.moveToNext();  
                    }  
                }  
            }  
        }  

        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            SmsContent content = new SmsContent(new Handler());  
            //注册短信变化监听  
            this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);  
        }  
    }  

自己写的

public class MainActivity extends Activity {
    private Handler handler=new Handler();
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView=(TextView) findViewById(R.id.textview_logcat);
      SmsContent smsContent=new SmsContent(handler);
      this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, smsContent);  
    }
    /**
     * 监听短信数据库 20151124
     */
    class SmsContent extends ContentObserver {
        private Cursor cursor = null;

        public SmsContent(Handler handler) {
            super(handler);

        }

        @SuppressWarnings("deprecation")
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);

            cursor = managedQuery(Uri.parse("content://sms/inbox"),
                    new String[] { "_id", "address", "read", "body","date" },
                    " address=? and read=?", new String[] { "10086", "0" },
                    "_id desc");

            if (cursor != null && cursor.getCount() > 0) {
                cursor.moveToNext();
                // /
                ContentValues values = new ContentValues();
                values.put("read", "1");
                getContentResolver().update(Uri.parse("content://sms/inbox"),
                        values, " _id=?",
                        new String[] { "" + cursor.getInt(0) });
                // /
                int smsbodyColumn = cursor.getColumnIndex("body");
                String smsBody = cursor.getString(smsbodyColumn);
                String date=cursor.getString(cursor.getColumnIndex("date"));
                long l=Long.parseLong(date);
                SimpleDateFormat format=new SimpleDateFormat("yyyy/mm/dd HH:mm:ss");
                String smsDate=format.format(l);

                mTextView.setText(smsDate+smsBody);
                Toast.makeText(getApplicationContext(), smsDate+smsBody,Toast.LENGTH_SHORT).show();
//              String smsCode = MatchesUtil.getDynamicPassword(smsBody);
//              if (smsCode != null) {
//                  mWebView.loadUrl4Push("javascript:getSMSCode( '" + smsCode+"   "+smsDate
//                          + "')");
//              }
            }
            if (Build.VERSION.SDK_INT < 14) {
                cursor.close();
            }
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值