天天看點

Android手機短信攔截---Observer(二)

這種方法是通過監控手機的短信資料庫content://sms實作的。

1.實作Observer

public class SMSObserver extends ContentObserver

{
    ...
@Override

    public void onChange(boolean selfChange)

    {

       Log.i(TAG, "onChange : " + selfChange + "; " + MAX_ID + "; " + SELECTION);

       super.onChange(selfChange);

       try {

           Cursor cursor = mResolver.query(SMS.CONTENT_URI, PROJECTION, String.format(SELECTION, MAX_ID), null, null);

       Log.i(TAG,"cursor numbers :"+ cursor.getCount() );

        int id, type, protocol;

        Long thread_id;

        String phone, body;

        Message message;

        MessageItem item;



        int iter = ;

        boolean hasDone = false;



        while (cursor.moveToNext())

            {

                id = cursor.getInt(COLUMN_INDEX_ID);

                thread_id = cursor.getLong(COLUMN_THREAD_ID);

                type = cursor.getInt(COLUMN_INDEX_TYPE);

                phone = cursor.getString(COLUMN_INDEX_PHONE);

                body = cursor.getString(COLUMN_INDEX_BODY);

                protocol = cursor.getInt(COLUMN_INDEX_PROTOCOL);


                Log.i(TAG,"content : "+body+" protocol : "+ protocol);


                if (hasDone)

                {

                    MAX_ID = id;

                    break;

                }

                Log.i(TAG," body.startsWith : "+ body.startsWith(SMS.FILTER));
                if (protocol == SMS.PROTOCOL_SMS && body != null
                        && body.startsWith(SMS.FILTER))

                {//本示例程式是把以SMS.FILTER打頭的短信視為垃圾短信

                    hasDone = true;

                    item = new MessageItem();

                    item.setId(id);

                    item.setThreadId(thread_id);

                    item.setType(type);

                    item.setPhone(phone);

                    item.setBody(body);

                    item.setProtocol(protocol);

                    message = new Message();

                    message.obj = item;

                    mHandler.sendMessage(message);

                    Log.i(TAG,"send to handler;");
                }

                else

                {

                    if (id > MAX_ID)
                        MAX_ID = id;

                }

                if (iter > MAX_NUMS)
                    break;

                iter++;

            }
            cursor.close();
    } catch (Exception e) {
         e.printStackTrace();

    }
}

}
           

2.實作Handler

public class SMSHandler extends Handler

{

    public static final String TAG = "SMSHandler";



    private Context mContext;



    public SMSHandler(Context context)

    {

       super();

       this.mContext = context;

    }



    public void handleMessage(Message message)

    {

       Log.i(TAG,  "handleMessage: " + message);



       MessageItem item = (MessageItem) message.obj;

       //delete the sms

       Uri uri = ContentUris.withAppendedId(Uri.parse(SMS.CONTENT_URI+"/conversations"), item.getThreadId());

       int iResult=mContext.getContentResolver().delete(uri, "_id="+item.getId(), null);

       Log.i(TAG,  "delete sms item: " + uri.toString()+"(id="+item.getId()+"), result: "+iResult);

    }

}
           

注:

  1. 本方法同樣僅适用于Android4.3及以下版本
  2. 在Android4.4及以上版本,如果不為短信的default app,則僅僅能探測到垃圾短信,但在删除垃圾短信時會失敗(除非通過安卓的AppOps給本軟體賦予了短信寫的權限)。