开发者

Problem with SMS Broadcast receiver

Hey guys I could really use some insight into this one. Ok, I'm new to eclipse and android first of all. Here is what I've got. Ok, I've got a BroadcastReceiver that I'm using to monitor text messages. When a text message comes in it grabs the from and message body and then passes them on to method FilterMessage() this then does a query against the database using the message body.

So far it compiles but when I send the Text it doesn't trigger the BroadcastReceiver (doesn't print to logcat) here is the code:

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

                }

                Lo开发者_JS百科g.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);
        }


    }


}

here is the 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>


You need to use a capital T in your intent filter in the manifest:

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


Do not forget to use

<receiver android:exported="true" >
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜