从BroadcastReceiver发送SMS时崩溃

时间:2011-08-10 23:34:08

标签: android

我有一个BroadcastReceiver,当收到短信时,它会查询数据库并从数据库结果中发出适当的短信。我遇到的问题是,当我到达发送短信的部分时,它会不断崩溃

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) {
        String myPath = "/data/data/com.RealtorSMS/databases/RealtorDB.db";
        db = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

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

    }

}

这是logcat:

08-10 18:19:33.796: ERROR/ActivityManager(68): ANR in com.RealtorSMS
08-10 18:19:33.796: ERROR/ActivityManager(68): Reason: Broadcast of Intent { act=android.provider.Telephony.SMS_RECEIVED cmp=com.RealtorSMS/.SMSReceiver (has extras) }
08-10 18:19:33.796: ERROR/ActivityManager(68): Load: 0.56 / 0.32 / 0.28
08-10 18:19:33.796: ERROR/ActivityManager(68): CPU usage from 5091ms to 0ms ago:
08-10 18:19:33.796: ERROR/ActivityManager(68):   82% 583/com.RealtorSMS: 81% user + 0.9% kernel / faults: 150 minor
08-10 18:19:33.796: ERROR/ActivityManager(68):   7.6% 68/system_server: 4.9% user + 2.7% kernel / faults: 5 minor
08-10 18:19:33.796: ERROR/ActivityManager(68):   5.5% 139/com.android.systemui: 5.1% user + 0.3% kernel / faults: 85 minor
08-10 18:19:33.796: ERROR/ActivityManager(68):   1.3% 136/com.android.phone: 0.5% user + 0.7% kernel / faults: 15 minor
08-10 18:19:33.796: ERROR/ActivityManager(68):   0.7% 41/adbd: 0% user + 0.7% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68):   0.5% 411/com.android.mms: 0.3% user + 0.1% kernel / faults: 2 minor
08-10 18:19:33.796: ERROR/ActivityManager(68): 99% TOTAL: 93% user + 5.7% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68): CPU usage from 1582ms to 2224ms later:
08-10 18:19:33.796: ERROR/ActivityManager(68):   88% 583/com.RealtorSMS: 88% user + 0% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68):     88% 583/com.RealtorSMS: 88% user + 0% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68):   12% 68/system_server: 6.2% user + 6.2% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68):     12% 81/ActivityManager: 7.8% user + 4.6% kernel
08-10 18:19:33.796: ERROR/ActivityManager(68): 100% TOTAL: 93% user + 6.2% kernel

1 个答案:

答案 0 :(得分:0)

您可能需要将处理逻辑移动到单独的服务中:而不是在FilterMessage方法中调用onReceive,调用startService并在单独的线程中执行处理。 ANR是因为FilterMessage函数的某些部分花了太长时间:我希望你的数据库打开/循环/关闭花费的时间太长。