使用广播接收器的短信过滤器

时间:2012-06-30 14:29:29

标签: android sms broadcastreceiver

我正在尝试隐藏收件箱中的一些邮件,然后转而将其放在新数据库中......我的代码如下SmsFilter.java

public class SmsFilter extends BroadcastReceiver{

SQLiteDatabase mydb,messagedb;
String number;
ArrayList<String> private_num = new ArrayList<String>();

@Override
public void onReceive(Context context, Intent intent) {

//private named database contains numbers those which i wish to hide

    mydb=context.openOrCreateDatabase("private", 0,null);
    mydb.execSQL("CREATE TABLE IF NOT EXISTS tab1(num INT(13))");


    Cursor  cur=mydb.rawQuery("select * from tab1", null);
    while(cur.moveToNext())
    {
        number=cur.getString(cur.getColumnIndex("num"));

        private_num.add(number);
    }

    if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle extras = intent.getExtras();

        if (extras != null) {

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

            if (pdus.length < 1) return;

            StringBuilder sb = new StringBuilder();
            String sender = null;

            for (int i = 0; i < pdus.length; i++) {

                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);

                if (sender == null) sender = message.getOriginatingAddress();

                String text = message.getMessageBody();
                if (text != null) sb.append(text);

            }
            for(int i=0;i<private_num.size();i++)
            {
                if (sender.equals(private_num.get(i))) {

             // create a new database and insert messages

                    messagedb=context.openOrCreateDatabase("message",0, null);
                    messagedb.execSQL("CREATE TABLE IF NOT EXISTS tab2(sender INT(13),body varchar)");
                    mydb.execSQL("INSERT INTO tab2 VALUES('"+sender+"','"+sb+"')");

                    abortBroadcast();
                }

            }

        }
    }

}}

也表现如下

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

<receiver android:name=".SmsFilter">

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

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

            </intent-filter>

    </receiver>

但是我无法通过这个接收器隐藏我的信息......原因是什么?.....任何人都帮助我......我遇到了麻烦......

2 个答案:

答案 0 :(得分:0)

将优先级提高到可能的最高整数.. “intent-filter android:priority =”2 ^ 32-1“

答案 1 :(得分:0)

更改您的代码:

<receiver android:name=".SmsFilter">

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

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

        </intent-filter>

</receiver>