1、系统的有序广播级别是重-1000~1000 设置我1000 在 onReceive 中调用abortBroadcast()方法 拦截不了短信的通知。查资料说同等级别动态设置优先使用广播 我试过 也不行 。
2、使用ContentObserver 观察模式监听短信是否改变
这个方式可以获取到所有的收件箱的短信,但是修改不了短信的状态。 没有权限
以下是我的代码:
第二种使用动态
2、使用ContentObserver 观察模式监听短信是否改变
这个方式可以获取到所有的收件箱的短信,但是修改不了短信的状态。 没有权限
以下是我的代码:
1
2
3
4
5
6
7
|
<receiver android:name=
"com.javen.receiver.SMSReceiver"
android:exported=
"true"
android:permission=
"android.permission.BROADCAST_SMS"
>
<intent-filter android:priority=
"1000"
>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED"
/>
<category android:name=
"android.intent.category.DEFAULT"
/>
</intent-filter>
</receiver>
|
第二种使用动态
1
2
3
4
|
IntentFilter filter =
new
IntentFilter(SMSReceiver.SMS_RECEIVED_ACTION);
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
//设置权限 2147483647
SMSReceiver myService =
new
SMSReceiver();
registerReceiver(myService, filter);
|
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
package
com.javen.receiver;
import
java.text.SimpleDateFormat;
import
java.util.Date;
import
android.content.BroadcastReceiver;
import
android.content.Context;
import
android.content.Intent;
import
android.database.Cursor;
import
android.net.Uri;
import
android.os.Bundle;
import
android.telephony.SmsMessage;
import
android.widget.Toast;
public
class
SMSReceiver
extends
BroadcastReceiver {
public
static
final
String SMS_RECEIVED_ACTION =
"android.provider.Telephony.SMS_RECEIVED"
;
@Override
public
void
onReceive(Context context, Intent intent) {
System.out.println(
"SMSReceiver, isOrderedBroadcast()="
+ isOrderedBroadcast());
System.out.println(
"接收短信执行了"
);
Toast.makeText(context,
"开始接收短信"
,
1
).show();
String action = intent.getAction();
if
(SMS_RECEIVED_ACTION.equals(action)) {
SmsMessage msg =
null
;
Bundle bundle = intent.getExtras();
if
(bundle !=
null
) {
Object[] pdusObj = (Object[]) bundle.get(
"pdus"
);
for
(Object p : pdusObj) {
msg = SmsMessage.createFromPdu((
byte
[]) p);
String msgTxt = msg.getMessageBody();
// 得到消息的内容
Date date =
new
Date(msg.getTimestampMillis());
// 时间
SimpleDateFormat format =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
String receiveTime = format.format(date);
String senderNumber = msg.getOriginatingAddress();
if
(senderNumber.equals(
"5556"
)) {
System.out.println(
"发送者是556"
);
abortBroadcast();
}
if
(senderNumber.equals(
"15555215556"
)) {
System.out.println(
"发送者是15555215556"
);
abortBroadcast();
}
if
(msgTxt.equals(
"123"
)) {
System.out.println(
"发送的内容为 123"
);
Toast.makeText(context,
"abortBroadcast"
,
Toast.LENGTH_LONG).show();
abortBroadcast();
}
else
{
Toast.makeText(context, msgTxt, Toast.LENGTH_LONG)
.show();
System.out.println(
"发送人:"
+ senderNumber +
" 短信内容:"
+ msgTxt +
"接受时间:"
+ receiveTime);
}
}
}
}
}
public
void
deleteSMS(Context context, String smscontent) {
try
{
// 准备系统短信收信箱的uri地址
Uri uri = Uri.parse(
"content://sms/inbox"
);// 收信箱
// 查询收信箱里所有的短信
Cursor isRead = context.getContentResolver().query(uri,
null
,
"read="
+
0
,
null
,
null
);
while
(isRead.moveToNext()) {
// String phone =
// isRead.getString(isRead.getColumnIndex("address")).trim();//获取发信人
String body = isRead.getString(isRead.getColumnIndex(
"body"
))
.trim();
// 获取信息内容
if
(body.equals(smscontent)) {
int
id = isRead.getInt(isRead.getColumnIndex(
"_id"
));
int
delid=context.getContentResolver().delete(
Uri.parse(
"content://sms"
),
"_id="
+ id,
null
);
System.out.println(
"删除没有》》》"
+delid);
}
}
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
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
|
package
com.javen.holdupmessage;
import
android.content.Context;
import
android.database.ContentObserver;
import
android.database.Cursor;
import
android.net.Uri;
import
android.os.Handler;
import
android.util.Log;
public
class
MyContentObserver
extends
ContentObserver {
private
Context mcontext;
public
MyContentObserver(Handler handler, Context context) {
super
(handler);
this
.mcontext = context;
}
@Override
public
void
onChange(
boolean
selfChange) {
super
.onChange(selfChange);
// 读取发件箱中的短信,数据库中是以时间降序来存储短信的,所以,第一条记录就是最新的记录
// 如果把 content://sms/outbox 改成 content://sms/inbox,则可以读取收件箱中的短信
Cursor cursor =
this
.mcontext.getContentResolver().query(
Uri.parse(
"content://sms/inbox"
),
null
,
null
,
null
,
null
);
// 如果要读取指定号码的未读短信,则
// Cursor cursor =
// this.mcontext.getContentResolver().query(Uri.parse("content://sms/inbox"),
// null,
// " address=? and read=?", new String[]{"123456", "0"}, null);
// 其中address为号码,对应 123456;read为已读/未读标志,0为未读,1为已读; body为短信内容
StringBuilder sb =
new
StringBuilder();
while
(cursor.moveToNext()) {
// _id为短信编号;address为手机号码;body为短信内容;time为时间,长整型的
sb.append(
"_id="
).append(
cursor.getInt(cursor.getColumnIndex(
"_id"
)));
sb.append(
";address="
).append(
cursor.getString(cursor.getColumnIndex(
"address"
)));
sb.append(
";body="
).append(
cursor.getString(cursor.getColumnIndex(
"body"
)));
sb.append(
";time="
).append(
cursor.getLong(cursor.getColumnIndex(
"date"
)));
sb.append(
";read="
).append(
cursor.getInt(cursor.getColumnIndex(
"read"
)));
sb.append(
"\n"
);
}
System.out.println(
"删除》》"
+
this
.mcontext.getContentResolver().delete(
Uri.parse(
"content://sms"
),
"_id="
+
1
,
null
));
Log.i(
"ReceiveSendSMS"
, sb.toString());
}
}
|