最近研究了一下如何对Android发送短信进行监控,首先考虑到是否会有广播机制,查了一下api文档发现没有,到了网上查了半天也没用解决办法,主要问题还是Android没有提供这中监听机制,怎么办呢,诶,苦想了几天,想到了一个还算可行的方法,但是只能对系统短信进行监控,还是不能对第三方进行监控,没办法,好了下面介绍一下我的实现方法。主要是利用Android对contentProvider内容变化监听方法实现ContentObserver类,

具体如下;

 

  1. package com.listener.app; 
  2.  
  3. import java.text.SimpleDateFormat; 
  4. import java.util.Date; 
  5.  
  6. import android.app.Service; 
  7. import android.content.ContentResolver; 
  8. import android.content.Intent; 
  9. import android.database.ContentObserver; 
  10. import android.database.Cursor; 
  11. import android.net.Uri; 
  12. import android.os.Handler; 
  13. import android.os.IBinder; 
  14. import android.util.Log; 
  15.  
  16. publicclass SmsStatus extends Service{ 
  17.      
  18.      
  19.     @Override 
  20.     publicvoid onCreate() { 
  21.         //为content://sms的数据改变注册监听器 
  22.         getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, new SmsObserver(new Handler())); 
  23.          
  24.         super.onCreate(); 
  25.     } 
  26.     // ContentObserver监听器类 
  27.     privatefinalclass SmsObserver extends ContentObserver{ 
  28.          
  29.         public SmsObserver(Handler handler){ 
  30.             super(handler); 
  31.         } 
  32.         publicvoid onChange(boolean selfChange){ 
  33.              
  34.             sendMsg(); 
  35.             
  36.         } 
  37.          
  38.         privatevoid sendMsg(){ 
  39.             Cursor cursor = getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null); 
  40.             if (cursor.moveToFirst()){ 
  41.                 StringBuilder sb = new StringBuilder(); 
  42.                 // 获取短信的发送地址 
  43.                 sb.append("address=").append( 
  44.                     cursor.getString(cursor.getColumnIndex("address")));                     
  45.                 // 获取短信的标题 
  46.                 sb.append('\n'+"subject=").append( 
  47.                     cursor.getString(cursor.getColumnIndex("subject"))); 
  48.                 // 获取短信的内容 
  49.                 sb.append('\n'+"body=").append( 
  50.                     cursor.getString(cursor.getColumnIndex("body"))); 
  51.                 // 获取短信的发送时间 
  52.                  SimpleDateFormat dateFormat = new SimpleDateFormat(    
  53.                             "yyyy-MM-dd hh:mm:ss");    
  54.                     Date d = new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date"))));    
  55.                    String date = dateFormat.format(d);  
  56.                 sb.append('\n'+"time=").append(date); 
  57.                 Log.e("msg", "Sent SMS:" + sb.toString()); 
  58.             } 
  59.             if(cursor!=null){ 
  60.                 cursor.close(); 
  61.                 cursor=null
  62.             } 
  63.         } 
  64.      
  65.      
  66.     @Override 
  67.     public IBinder onBind(Intent intent) { 
  68.         returnnull
  69.     }    
  70.      
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类中,以确保内容为时时监听。