最近研究了一下如何对Android发送短信进行监控,首先考虑到是否会有广播机制,查了一下api文档发现没有,到了网上查了半天也没用解决办法,主要问题还是Android没有提供这中监听机制,怎么办呢,诶,苦想了几天,想到了一个还算可行的方法,但是只能对系统短信进行监控,还是不能对第三方进行监控,没办法,好了下面介绍一下我的实现方法。主要是利用Android对contentProvider内容变化监听方法实现ContentObserver类,
具体如下;
- package com.listener.app;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.app.Service;
- import android.content.ContentResolver;
- import android.content.Intent;
- import android.database.ContentObserver;
- import android.database.Cursor;
- import android.net.Uri;
- import android.os.Handler;
- import android.os.IBinder;
- import android.util.Log;
- publicclass SmsStatus extends Service{
- @Override
- publicvoid onCreate() {
- //为content://sms的数据改变注册监听器
- getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, new SmsObserver(new Handler()));
- super.onCreate();
- }
- // ContentObserver监听器类
- privatefinalclass SmsObserver extends ContentObserver{
- public SmsObserver(Handler handler){
- super(handler);
- }
- publicvoid onChange(boolean selfChange){
- sendMsg();
- }
- privatevoid sendMsg(){
- Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
- if (cursor.moveToFirst()){
- StringBuilder sb = new StringBuilder();
- // 获取短信的发送地址
- sb.append("address=").append(
- cursor.getString(cursor.getColumnIndex("address")));
- // 获取短信的标题
- sb.append('\n'+"subject=").append(
- cursor.getString(cursor.getColumnIndex("subject")));
- // 获取短信的内容
- sb.append('\n'+"body=").append(
- cursor.getString(cursor.getColumnIndex("body")));
- // 获取短信的发送时间
- SimpleDateFormat dateFormat = new SimpleDateFormat(
- "yyyy-MM-dd hh:mm:ss");
- Date d = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));
- String date = dateFormat.format(d);
- sb.append('\n'+"time=").append(date);
- Log.e("msg", "Sent SMS:" + sb.toString());
- }
- if(cursor!=null){
- cursor.close();
- cursor=null;
- }
- }
- @Override
- public IBinder onBind(Intent intent) {
- returnnull;
- }
- }
package com.listener.app; import java.text.SimpleDateFormat; import java.util.Date; import android.app.Service; import android.content.ContentResolver; import android.content.Intent; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; import android.os.Handler; import android.os.IBinder; import android.util.Log; public class SmsStatus extends Service{ @Override public void onCreate() { //为content://sms的数据改变注册监听器 getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, new SmsObserver(new Handler())); super.onCreate(); } // ContentObserver监听器类 private final class SmsObserver extends ContentObserver{ public SmsObserver(Handler handler){ super(handler); } public void onChange(boolean selfChange){ sendMsg(); }
} private void sendMsg(){ Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null); if (cursor.moveToFirst()){ StringBuilder sb = new StringBuilder(); // 获取短信的发送地址 sb.append("address=").append( cursor.getString(cursor.getColumnIndex("address"))); // 获取短信的标题 sb.append('\n'+"subject=").append( cursor.getString(cursor.getColumnIndex("subject"))); // 获取短信的内容 sb.append('\n'+"body=").append( cursor.getString(cursor.getColumnIndex("body"))); // 获取短信的发送时间 SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss"); Date d = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date")))); String date = dateFormat.format(d); sb.append('\n'+"time=").append(date); Log.e("msg", "Sent SMS:" + sb.toString()); } if(cursor!=null){ cursor.close(); cursor=null; } } @Override public IBinder onBind(Intent intent) { return null; } }
以上有几点需要注意;
权限:
<uses-permission android:name="android.permission.READ_SMS"/>
1,content必须为content://sms/不能改为content://sms/outbox,即系统只能对整个短信的content进行监控而不能对content中item进行监控
2,ContentObserver监听时最好放置在service类中,以确保内容为时时监听。