SMS广播接收器的问题

时间:2011-08-10 22:12:50

标签: android

嘿伙计们,我真的可以对这一点有所了解。好的,我是eclipse和android的新手。这就是我所拥有的。好的,我有一个我用来监控短信的BroadcastReceiver。当文本消息进入时,它会抓取from和message正文,然后将它们传递给方法FilterMessage(),然后使用消息正文对数据库进行查询。

到目前为止它编译但是当我发送文本它不会触发BroadcastReceiver(不打印到logcat)这里是代码:

public class SMSReceiver extends BroadcastReceiver {
    SQLiteDatabase db;
    private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
    private static String messagefrom;
    private static String messagebody;
    final String dbTable = "Realtor_SMS_Table";


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

        if (intent != null && intent.getAction() != null && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
                SmsMessage[] messages = new SmsMessage[pduArray.length];
                for (int i = 0; i < pduArray.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[])pduArray[i]);
                    messagefrom = messages[i].getOriginatingAddress();
                    messagebody = messages[i].getMessageBody();
                    Log.d("My SMSReceiver", "From: " + messagefrom);
                    Log.d("My SMSReceiver", "Msg: " + messagebody);
                    FilterMessage(dbTable, messagefrom, messagebody);

                }

                Log.d("My SMSReceiver", "SMSReceived");

        }
    }
    public void FilterMessage(String dbTable, String messagefrom, String messagebody) {

        DBHelper dbhelper = new DBHelper(null);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor cursor = db.query(dbTable, new String[] {"_id", "Description", "URL"}, 
                "_id like " + "'%" + messagebody + "%'", null, null, null, null);


        Log.d("My SMSReceiver", "sql= " +cursor);

        if (cursor != null) {
            cursor.moveToFirst();
            String messageAddress = null;
            String messageDescription = null;
            String messageURL = null;
            while (cursor.isAfterLast() == false){

                messageAddress = cursor.getString(0);
                messageDescription = cursor.getString(1);
                messageURL = cursor.getString(2);

            }
            SmsManager sm = SmsManager.getDefault();
            sm.sendTextMessage(messagefrom, null, messageAddress + messageDescription, null, null);
            sm.sendTextMessage(messagefrom, null, messageAddress + messageURL, null, null);
        }


    }


}

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.RealtorSMS" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ActivationActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <receiver android:name=".SMSReceiver">
            <intent-filter>
                <action android:name="android.provider.telephony.SMS_RECEIVED"></action>
            </intent-filter>
        </receiver>



        <activity android:name=".PreferencesAcitvity" />
        <activity android:name=".ViewListingsActivity" />


    </application>
</manifest>

2 个答案:

答案 0 :(得分:1)

您需要在清单中的intent过滤器中使用大写字母T:

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

答案 1 :(得分:0)

别忘了使用

<receiver android:exported="true" >