nexus5 android5.0 读取本机短信

由于项目需要读取本机短信进行自动填充短信验证码,手上是一台nexus5 android5.0版本,一开始的代码如下:@SuppressLint("NewApi")
public void BtnOnClick(View view){
ContentResolver cr = this.getContentResolver();
String[] projection = new String[] { "body","address" };
String where = "type=1 and address ="'15299999999'";
Cursor cur = cr.query(Sms.CONTENT_URI, projection, where, null, "_id desc" + " limit 10");
        int i=cur.getCount();
while(cur.moveToNext()) {
smsBody = cur.getString(cur.getColumnIndex("body"));
address = cur.getString(cur.getColumnIndex("address"));

}
cur.close();

}
死活读取不出短信,另一台红米手机却是可以的,还以为是android5.0没有开放权限,后来才知道nexus5是国际版,中国大陆的手机号要加+86,将查询条件改为如下就可以正确读取出来:String where = "type=1 and address ="'+8615299999999'";type=1表示收到的短信还有一个问题当你要查询多个手机号可以用如下语句:String where = "type=1 and address in (10086,10659843)";
这样是没有问题的,但是如下是查不出来的String where = "type=1 and address in (+8610086,+8610659843)";
因为加号的原因,应该改成下面这样String where = "type=1 and address in ('+8610086','+8610659843')";
所以不管有没有特殊符号最好养成良好的习惯加上单引号,下面附上代码,楼主懒,没有界面,可以在调试模式下运行跟踪值得变化

猜你喜欢

转载自lisj10659.iteye.com/blog/2178429
今日推荐