现在的位置: 首页 > 综合 > 正文

android手机短信拦截的实现

2013年10月10日 ⁄ 综合 ⁄ 共 2708字 ⁄ 字号 评论关闭

 

以前刚开始接触android时对诸如短信拦截之类的功能很感兴趣,网上很多文章都介绍了使用广播接收android.provider.Telephony.SMS_RECEIVED的动作,从而接收到SMS信息,不过当时也不知道怎么样把这个广播删掉,从而实现一个类似短信黑名单的功能。后来在网上看到可以使用abortBroadcast来屏蔽广播,我测试了一下,可行。

 

先上源码:

 

AndroidManifest.xml的代码:

 

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.study.sms"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

<receiver android:name=".smsReceiver">

<intent-filter android:priority="1000">

<action android:name="android.provider.Telephony.SMS_RECEIVED"/>

</intent-filter>

</receiver>

    </application>

<uses-permission android:name="android.permission.RECEIVE_SMS">

</uses-permission>

</manifest> 

 

 

 

smsReceiver的代码:

 

 

package com.study.sms;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.os.Bundle;

import android.telephony.SmsMessage;


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) {

String action = intent.getAction();

if (SMS_RECEIVED_ACTION.equals(action)){

Bundle bundle = intent.getExtras();

if (bundle != null){

Object[] pdus = (Object[])bundle.get("pdus");

for (Object pdu : pdus){

SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);

String sender = message.getOriginatingAddress();

if ("5556".equals(sender)){

//屏蔽手机号为5556的短信,这里还可以时行一些处理,如把这个信息发送到第三人的手机等等。

abortBroadcast();

}

}

}

}

}


}

 

测试的效果图:

 

 

使用5556给5554发送短信,5554无法收到。同时再起一个5558的avd给5554发送短信,一切正常。

 

之所以这个程序能实现屏蔽系统短信的功能,是因为广播有两种不同的类型:普通广播(normal broadcasts)和有序广播(ordered broadcasts)。普通广播是完全异步的,可以被所有的接收者接收到,并且接收者无法终止广播的传播。然而有序广播是按照接收者声明的优先级别,被接收者依次接收到。优先级别声明在 intent-filter 元素的 android:priority 属性中,数越大优先级别越高,取值范围:-10001000,优先级别也可以调用IntentFilter对象的setPriority()进行设置。有序广播的接收者可以终止广播Intent的传播,广播Intent的传播一旦终止,后面的接收者就无法接收到广播。上面的实例就是将smsReceiver的优先级设为1000。

 

 

 

Context.sendBroadcast()

发送的是普通广播,所有订阅者都有机会获得并进行处理。

Context.sendOrderedBroadcast()

发送的是有序广播,系统会根据接收者声明的优先级别按顺序逐个执行接收者。

 

我查看了一下系统发送android.provider.Telephony.SMS_RECEIVED广播的源码SMSDispatcher.java(frameworks/base/telephony/java/com/android/internal/telephony),证实其发送的是有序广播。

 

    protected void dispatchPdus(byte[][] pdus) {

        Intent intent = new Intent(Intents.SMS_RECEIVED_ACTION);

        intent.putExtra("pdus", pdus);

        dispatch(intent, "android.permission.RECEIVE_SMS");

    }

   public void dispatch(Intent intent, String permission) {
        // Hold a wake lock for WAKE_LOCK_TIMEOUT seconds, enough to give any
        // receivers time to take their own wake locks.
        mWakeLock.acquire(WAKE_LOCK_TIMEOUT);
        mContext.sendOrderedBroadcast(intent, permission, mResultReceiver,
                this, Activity.RESULT_OK, null, null);
    }

 

 

抱歉!评论已关闭.