-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmsObserver.java
More file actions
executable file
·74 lines (67 loc) · 2.4 KB
/
SmsObserver.java
File metadata and controls
executable file
·74 lines (67 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.youth.xframe.utils;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
短信的Uri共有一下几种:
content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱 (正在发送的信息)
content://sms/failed 发送失败
content://sms/queued 待发送列表 (比如开启飞行模式后,该短信就在待发送列表里)
需要权限
<uses-permission android:name="android.permission.READ_SMS" />
*/
public class XSmsObserver extends ContentObserver {
public static final String SMS_URI_INBOX = "content://sms/inbox";
private Activity activity = null;
private String smsContent = "";
private SmsListener listener;
private String like;
public XSmsObserver(Activity activity, Handler handler,String like, SmsListener listener) {
super(handler);
this.activity = activity;
this.listener = listener;
this.like=like;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Cursor cursor = null;
// 读取收件箱中含有某关键词的短信
ContentResolver contentResolver = activity.getContentResolver();
cursor = contentResolver.query(Uri.parse(SMS_URI_INBOX), new String[] {
"_id", "address", "body", "read" }, "body like ? and read=?",
new String[] { "%"+like+"%", "0" }, "date desc");
if (cursor != null) {
cursor.moveToFirst();
if (cursor.moveToFirst()) {
String smsBody = cursor.getString(cursor.getColumnIndex("body"));
String regEx = "[^0-9]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(smsBody.toString());
smsContent = m.replaceAll("").trim().toString();
if (!XEmptyUtils.isSpace(smsContent)) {
listener.onResult(smsContent);
}
}
}
}
/*
* 短信回调接口
*/
public interface SmsListener {
/**
* 接受sms状态
*
* @Title: onResult
*/
void onResult(String smsContent);
}
}